JavaScript controls whether a new window opens for a site link

Source: Internet
Author: User
Tags setcookie

However, in the verification standard of the target=, the "_blank" attribute is not validated.

The reason is that those who think of the brick to open a new window without the consent of the user, such behavior belongs to strong X user's thought. Is unfriendly, impolite behavior. So the standard proposed is to use rel= "external" to declare, with a external.js to achieve. Here to paste this JS file code, need friends to take away.

The code is as follows Copy Code

function Externallinks () {
if (!document.getelementsbytagname) return;
var anchors = document.getElementsByTagName ("a");
for (var i=0; i<anchors.length; i++) {
var anchor = Anchors[i];
if (Anchor.getattribute ("href") &&
Anchor.getattribute ("rel") = = "External nofollow")
Anchor.target = "_blank";
}
}
Window.onload = Externallinks;

But the above is not the main content of this article, here I mainly introduce a new method:

Through JS to enable users to choose whether to open the link in a new window: This code can record cookies, save 365 days. That is, the user chooses to save this setting for a certain amount of time without deleting the cookie.

Here's a demo of the Code: (It's recommended to save as an HTML file to make it easier to see cookies)

The code is as follows Copy Code

<script type= "Text/javascript" >
Window.onload=checkcookie;
function GetCookie (c_name)
{
if (document.cookie.length>0)
{
C_start=document.cookie.indexof (c_name + "=")
if (c_start!=-1)
{
C_start=c_start + c_name.length+1
C_end=document.cookie.indexof (";", C_start)
if (c_end==-1) c_end=document.cookie.length
Return unescape (document.cookie.substring (c_start,c_end))
}
}
Return ""
}

function Setcookie (c_name,value,expiredays)
{
var exdate=new Date ()
Exdate.setdate (Exdate.getdate () +expiredays)
document.cookie=c_name+ "=" +escape (value) +
((expiredays==null)? "" : "; Expires= "+exdate.togmtstring ())
}

function Checkcookie ()
{
Target=getcookie (' target ')
if (Target!=null && target== "true")
{
document.getElementById ("Checkit"). Checked=true;
Openstyle ("_blank");
}
Else
{
if (Target!=null && target== "false")
{document.getElementById ("Checkit"). Checked=false;
Openstyle ("");}
}
}

function Openstyle (choice) {
var links = document.getElementsByTagName ("a");
for (Var i=0;i<links.length;i++) {
Links[i].target = choice;
}
}
function SetCheck (obj) {
if (obj.checked)
{
Openstyle ("_blank");
Setcookie (' target ', ' true ', 365);
}
else{
Openstyle ("");
Setcookie (' target ', ' false ', 365);
}
}
</script>
<body>

<input type= "checkbox" id= "Checkit" onclick= "SetCheck (this);"/> Choose whether to open <br/> in a new window
<a href= "Http://www.111cn.net" >baidu</a><br/>
<a href= "Http://www.111cn.net" > Android Theme </a>
</body>


Advanced section: Free to set up the station inside the station outside the link whether a new window open if some friends need to set up the link in the station in this window open, the links outside the station are all new windows, how to do?

First Add code:

The code is as follows Copy Code

var index=location.hostname; Get the current primary domain

Then modify the Openstyle function to:

The code is as follows Copy Code
function Openstyle (choice) {
var links = document.getElementsByTagName ("a");
for (Var i=0;i<links.length;i++) {
if (Links[i].href.indexof (index) ==-1 && links[i].href.indexof (' javascript ') = = 1) {
Links[i].target = choice;
}
}
}

OK, this is the end. This will be able to determine the site link in the current window open, outbound links in the new window opened.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.