2012-10-19 119 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在這裏工作,,,, –

相關問題