我假设你对HTML和HTTP不是很舒适,并且你正在学习,因此没有调查页面的源代码来了解发生了什么。
这完全没问题。
下面是该页面的源代码的简化版本:
<form id="tryitform" name="tryitform"
action="tryit_view.asp" method="post"
target="view"
onsubmit="validateForm();">
Source Code: <input type="button" value="Submit Code »"
onclick="submitTryit()">
Result: <textarea id="pre_code">
<!-- Your source code goes here -->
</textarea>
<input type="hidden" id="code" name="code" />
<input type="hidden" id="bt" name="bt" />
<iframe id="viewIFRAME" name="view"
src="tryit_view.asp?filename=tryjsref_loc_host">
</iframe>
</form>
<script type="text/javascript">
// This Script is called when you press the "Submit Code" button
function submitTryit()
{
// I'll omit the code, but what it does is this:
// 1. Take the text (your code) in the 'pre_code' textarea
// and substitute '=' with 'w3equalsign'
// and 'script' with 'w3scrw3ipttag'.
// 2. Put the modified text in the 'code' hidden input field
// 3. Change the form's action to "tryit_view.asp?x=" + a random number
// 4. Call validateForm(), which basically
// only checks if the code doesn't exceed 5000 characters
// 5. Submit form
}
window.onload = function(){ submitTryit() }
// Call submitTryit() when the page first loads
// so the view is filled also when you first arrive.
</script>
的核心思想在这里明白的是,你写的代码去哪里了action
属性被设置为“tryit_view.asp X窗体上? =“+一个随机数,并且target
属性设置为”查看“。此表单上的target
属性表示表单提交的结果应显示在iframe上,其中name
属性设置为“查看”。
所以你看,问题在于你的代码实际上并没有在浏览器上看到的页面上运行。它实际上是通过POST发送到服务器的。
服务器然后用不同的页面进行响应,该页面将填充iframe的内容。
iframe基本上是一个“嵌在矩形中的小型浏览器”,它嵌套在主页面内,它有一个不同的URL,它与主页的URL没有任何关系,并且不会出现在地址栏中。该网址代替了表单操作的网址。
现在大概是服务器对POST做出的回应是在恢复上面那些奇怪的文本替换之后,用你提交的代码创建一个HTML页面。
所以你的代码最终在iframe中运行。
因此,当您编写window.location
或只是location
时,您的代码将最终实际访问iframe的位置,而不是您的主页的位置。
你可以这样访问父页面的位置:
// 'parent', same as 'window.parent', refers to
// the frame which is the parent of the frame where
// this code is being run (the iframe named 'view').
document.write(parent.location.search);
或者这样:
// "top" is the frame at the top of the frame hierarchy
document.write(top.location.search);
祝您学习!
你想要什么?地理位置? – Toping
你的问题到底是什么? –
他的问题是,它应该在url中显示查询字符串 – atmd