我需要在所有博客链接中添加额外的单词。
我正在使用博客。
例如。
Url为:http://wwww.example.com/post1.html则它将自动更改为http://wwww.example.com/post1.html?extraword
意味着我需要添加所有链接?我的原始链接后的附加词。
我提到过。htaccess添加了一个额外的单词来添加我所有的URL,但它是关于htaccess的,但博客作者没有htaccess。
因此,请建议我使用javascript编写代码,并在我的所有url中添加一些额外的单词。
一个简单的方法(使用jQuery)是:
var word='?extraword';
$('a').each(function(){
var link=$(this).attr('href');
$(this).attr('href',link+word);
});
只需在jQuery脚本的开头包含这个小脚本,它就完成了。
更新:
如果链接是动态添加的,则必须在页面完全加载后更改其href
属性:
$(window).load(function(){
var word='?extraword';
$('a').each(function(){
var link=$(this).attr('href');
$(this).attr('href',link+word);
});
});
同时查看您博客的代码,我建议您将此脚本放在html代码的末尾,正好在关闭body
标记之前。
更新2:
<script>
$(window).load(function(){
var word='?extraword';
$('a').each(function(){
var thelink=$(this).attr('href');
$(this).attr('href',thelink+word);
});
});
</script>
更新3:
<script>
$(window).load(function(){
var word='?extraword';
$('a').each(function(){
var thelink=$(this).attr('href');
$(this).attr('href',thelink+word);
});
if(window.location.indexOf(word)<0){
window.location=window.location+word;
}
});
</script>
在java脚本中,您可以使用concat()将两个字符串连接在一起。
http://www.w3schools.com/jsref/jsref_concat_string.asp