2012-10-19 126 views
0

当过我的输出window.location.search它让我看到的结果是这样的:location.search是不是为我工作,因为它应该

X = 0.8690746387001127或任何其他数量

,但实际上它应该是这样的

?文件名= tryjsref_loc_search

任何人能解释为什么这样做呢?

这里是我的示例代码

//网页网址:http://w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_host

<!DOCTYPE html> 
<html> 
<body> 

<script> 

document.write(window.location.search) 

</script> 

</body> 
</html> 

的代码

X = 0.8690746387001127

+0

你想要什么?地理位置? – Toping

+0

你的问题到底是什么? –

+0

他的问题是,它应该在url中显示查询字符串 – atmd

回答

0

我假设你对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 &raquo;" 
         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); 

祝您学习!

+0

thankx老兄,,,真正的事情发生在幕后的真实情况,,, –

+0

我已经编辑了答案咯,主要是因为我有写了POST的错误URL,但也用于重新措辞并更好地解释一些部分(我希望)。但是,这是幕后发生的事情。如果您安装了Firebug或者其他可以让您查看HTTP流量的开发工具,则可以看到正在进行的POST以及服务器为您提供的答案。 – Zecc

2

首先,w3schools is not a good website to learn from输出。我建议使用jqFundamentals(“javascript基础知识”部分)

这就是说,window.location.search为您提供窗口的当前查询字符串。在w3school的“尝试”网站的情况下,这似乎是Math.rand(),通常用作cachebuster技术。

如果您在点击“提交”按钮时运行提琴手(或任何其他网络流量监视器),您将看到完整的URL为http://w3schools.com/jsref/tryit_view.asp?x=0.42147885356098413

MDN是JavaScript的文档的重要来源,其对window.location.search词条中说:

搜索| URL后面的部分?符号,包括?符号。 | ?q = devmo

+0

谢谢建议我会跟着,,,,,我可以将x = 0.42147885356098413转换回实际显示给我吗?如果是的话为什么 –

+0

你给我介绍了很多新东西,thankx再次竖起大拇指,,,, –

+0

我已经得到了关于x = 003403242304,iframes在这里工作,,,, –

相关问题