这种情况是,我想要一个脚本,将获取标签的自定义部分,并将其添加到一个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