JQ new window open link and set parameters

For links, in order to keep users on this site, we usually use a new window to open, you can set target="_blank". However, manual one is troublesome, and the other is likely to be missed. This article uses jQuery to query the link to be clicked, add relevant attributes, and make it open a new window.

 

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<a href="demo.php">Reading who asks you to recite, the water falls and the fragrance floats. </a>
<a href="demo.php?f=xxx">Reading who asks you to recite, the water falls and the fragrance floats. </a>
<a href="demo.php?f=xxx#45">Reading who asks you to recite, the water falls and the fragrance floats. </a>
<script src="jquery-1.10.1.min.js" ></script>
<script>
$(document).on('click','a',function(){
    var url = this.href,hash='';
    console.info(url);
    if(url.indexOf('#')>-1){
        hash = !!url.split('#')[1] ? '#' + url.split('#')[1] :'';
        url = url.split ('#') [0];       
    }
    if(url && url.substr(0,4) == 'http' && url.indexOf('onestopweb.cn')<0 ) {
        this.href = url.indexOf('?') > -1 ? url+'&utm_source=onestopweb.cn'+hash:url+'?utm_source=onestopweb.cn'+hash;
        this.setAttribute('target', '_blank');
    }
})
</script>
</body>
</html>

 

Effect picture:

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327097363&siteId=291194637