這種情況是,我想要一個腳本,將獲取標籤的自定義部分,並將其添加到一個URL。事情是這樣的:標籤過濾,從URL獲得ID
Label: www.wbsite1.com/something.php?id=1337
然後我想使腳本看法:id=1337
並將其添加到像這樣的鏈接:www.wbsite2.com/id/(car1337).html
林在javascript完整beguinner。
這種情況是,我想要一個腳本,將獲取標籤的自定義部分,並將其添加到一個URL。事情是這樣的:標籤過濾,從URL獲得ID
Label: www.wbsite1.com/something.php?id=1337
然後我想使腳本看法:id=1337
並將其添加到像這樣的鏈接:www.wbsite2.com/id/(car1337).html
林在javascript完整beguinner。
請問this能解決你的問題嗎?
可以讀取標籤的文字,而只提取字符串所需的部分,然後構造所需網址爲:
HTML:
<label>www.wbsite1.com/something.php?id=1337</label><br>
<button onclick='navigate();'>Go</button>
的JavaScript:
function navigate() {
var str = document.getElementsByTagName('label')[0].firstChild.data
str = str.substring(str.indexOf("id=")+3, str.length);
window.location.href = "http://www.wbsite2.com/id/car"+str+".html";
}
使用lastIndexOf()查找'?'從最後,該函數將返回'?'的索引。在url字符串中,然後你可以使用substring()來獲取'?'後的子字符串。該指數。
所以,在這裏你得到的子字符串,然後找到id ande值,你現在可以使用'+'連接幾個字符串。
<script language="javascript">
var url = "www.wbsite1.com/something.php?id=1337";
var index = url.lastIndexOf("?"); //get the position of the '?'
var str = url.substring(index+1); //get the string after url;
var _idx = str.indexOf("="); //get the '=' position.
var id = str.substring(0, _idx);
var value = str.substring(_idx);
var final_string = "www.wbsite2.com/"+id+"/(car"+value+").html";
alert(final_string);
</script>
很好的例子!容易理解:)謝謝隊友! – BlitZz
事情是這樣的:
//str is your Label value
var str_sub = str.substr(str.lastIndexOf("=")+1);
然後拼接所抓取的ID(1337)到要操縱其他字符串。
很好的基本示例:)謝謝 – BlitZz
非常好的例子,但有點難以理解:/ 它也看起來很好寫! – BlitZz