2016-07-07 60 views
0

enter image description here使用HTML和JavaScript

正如上面提到的,我需要創建一個將在錨標記可以使用,這將重定向和href值傳遞給我的另一頁即class="donate"傳遞href的值到另一個頁面 - mysite.com/donate.html,

donate.html將能夠獲得該值href值。 這樣當用戶點擊「繼續下載」就能下載 文件。

請讓我知道它是否可能。 我寧願只使用HTML和JavaScript。因爲我沒有託管我使用博客的網頁。

我見過templateism.com類似的概念,是這樣的:

www.templateism.com/p/preview.html?=www.templateism.com/10/2010/anypost.html

回答

0

基本上你需要通過在查詢字符串像mysite.com/donate.htm?key=value你所需要的值。

在donate.htm頁面,您可以使用這裏提到How can I get query string values in JavaScript?這個函數來獲取你的查詢字符串提供通過將鍵的值。

1

您可以像使用鏈接:

mysite.com/donate.html?www.yoursite.com.otherpage.html 

,然後使用javascript:

var href = document.location.href; 
var link = href.split('?')[1]; 
0

可以使用(需要jQuery的)JavaScript的下面的代碼的Click事件處理程序附加到所有鏈接與donate類。對page1.html和page2.html

JS

$(document).ready(function() { 
    $('a.donate').click(function() { 
     // Retrieve the href value and store it in a variable. 
     var href = $(this).attr('href'); 

     // Redirect a user to the desired page and passes the href value in query string. 
     window.location.href = 'http://example.com/donate.html?href=' + href; 

     // Ensure the original click event handler is not triggered. 
     return false; 
    }) 
}); 

點擊一個鏈接,用戶會被重定向到捐贈頁面,您可以解析查詢字符串並獲得href的值(後這就是前一頁中的<a href="...">)。

解析查詢字符串的方法有很多,但以下快速的&髒代碼應該適合您。在donate.html

JS

var location = document.location.href; 
var queryString = location.split('?')[1]; 

// In the href variable is now what you are looking for. 
var href = queryString.substr(5); 
+0

謝謝@Anton,如果你不介意的話,請給我一些小的教程,我仍然是初學者。我的意思是把這些線加起來。 –

+0

非常感謝@ aton,但它不適合我。 –

0

這裏嘗試一下本作例子......這是你的HTML代碼

<a href="http://example.com/donate.html?www.yoursite.com.otherpage.html"></a> 

正如Ashkan說,用他的JavaScript解析以前的URL從它... 將此代碼粘貼到donate.html

<script> 
var href = document.location.href; 
var link = href.split('?')[1]; 
window.location.href = "http://"+link;// This will show you the link 
</script> 

希望這有助於;

+0

謝謝@它的工作!並在'?'後分割網址你能告訴我們如何將用戶重定向到分割的url。我的意思是當他們點擊時如何將它們重定向到'var link'。 –

+0

@ShaymMurmu我編輯了我的答案。嘗試JavaScript現在它將工作..... –

+0

它重定向到父母目錄 實際的網址:www.myexample.com/rd/donate.html?www.anothersite.com 但它重定向到www.myexample.com /rd/www.anothersite.com 我需要:www.anothersite.com只有 –