Good _javascript tips for correcting specific Web content with external JavaScript

Source: Internet
Author: User
This code is written by oneself practicing to help others solve a problem.
The requirement is this:

<a href= "Http://www.jb51.net/article/window.open (", ")" >test</a&gt, this can not change, only to do things outside, how about it?

Request to click on the above connection, open a new page, and other actions on the original page.

Analysis: The above open a new page of the writing itself is wrong, can not open a new page. The correct wording should be:


<a href= "javascript:void (0)" onclick= "Http://www.jb51.net/article/window.open (", ")" >test</a>


So the solution is to use external JS to modify the content of the connection. The first thing to determine is that the href attribute inside the link contains "window.open" and then adds the onclick attribute to it and assigns the href content to it. Then modify the href attribute content.
But the solution found a problem, read the HREF attribute content can not get its actual content, but the Web page URL path (remove the last page file name) +href content, but also to the previous URL path removed. This can be solved with substring () combined with indexof ().
As for the original page, some content is much simpler.
According to the analysis, write the following test code to achieve the above requirements:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns =" http://www.w3.org/1999/xhtml "Xml:lang =" en "lang =" en "> <pead> <title>< /title> <script type = "Text/javascript" > Function addevent (elm,evtype,fn,usecapture) {if (Elm.adde Ventlistener) {Elm.addeventlistener (evtype,fn,usecapture); return true; }else if (elm.attachevent) {var r=elm.attachevent ("on" +EVTYPE,FN); return R; }else{alert ("Handler could not to be removed"); }} function DoOther () {if (event.srcelement.tagname== "A" && event.srcElement.getAttribute ("href"). In Dexof ("window.open")!=-1) {Event.srcElement.setAttribute ("onclick"), Eval (event.srcElement.href.substring ( Event.srcElement.href.lastIndexOf ("/") +1,event.srcelement.href.length)); Event.srcElement.setAttribute ("href", "javascript:void (0)"); document.getElementById ("Test"). Innerhtml= "<font color= #ff0000 ><b> is it like this? </b></font> "}} addevent (document," click ", DoOther); </script> </pead> <body> Test, this does not change, can only do things outside, how about it? <div id= "Test" ></div> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.