Replace target=_blank of anchor tag using javascrpt for w3c strict document type


If you are using strict mode as your html document type and if you have target=_blank in your html code then wc3 gives you an error. strict mode does not accept target=_blank and they suggest to use transitional mode.

here is the javascript replacement for target=_blank. before this you have to set the rel attribute of anchor tags to “external” or anything you wish because this is used in the javascript code so see which are all the anchor tags has set the rel attribute to external .

in other words you can set the rel attribute with some value to mean that these are the tags which has to be opened in a new window.

<script type="text/javascript">

        function target_blank() {

                var anchors = document.getElementsByTagName("a");
                for (var i=0; i<anchors.length; i++) 
		{
                        if ((anchor[i]href!='') && anchor[i].rel == "external")
                                anchor.target = "_blank";
                }
        }

        window.onload = traget_blank;

</script>

here is a script with which you can make a single anchor tag open in new window using javascript

<a href="http://host/path.html" 
onclick="window.open(this.href); return false;" 
onkeypress="window.open(this.href); return false;">
Open in new window using javascript</a>

Source vikku.info
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s