SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I get a url from location.href inside an onclick event?

    I am using this to set the new onclick event:

    <script type="text/javascript">
    var links = new Array();
    links = document.getElementsByTagName("td");

    for(var i=0;i<links.length;i++){
    var tempfunc = links[i].onclick;
    //*****EXTRACT THE URL*****//
    var url = "http://www.sitepoint.com"; //say this is the url you extracted
    links[i].onclick=function(){window.top.location.href=url};
    }
    </script>

    How do I get the URL from the current onclick event?

  2. #2
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tcovert View Post
    I am using this to set the new onclick event:

    <script type="text/javascript">
    var links = new Array();
    links = document.getElementsByTagName("td");

    for(var i=0;i<links.length;i++){
    var tempfunc = links[i].onclick;
    //*****EXTRACT THE URL*****//
    var url = "http://www.sitepoint.com"; //say this is the url you extracted
    links[i].onclick=function(){window.top.location.href=url};
    }
    </script>

    How do I get the URL from the current onclick event?
    Can this be helpful???

    <script type="text/javascript">
    function frmme(url)
    {
    alert('url'+url);
    }
    </script>
    </HEAD>

    <BODY>
    <table>
    <tr><td><a href="http://www.rediff.com" onclick="javascript: return frmme(this)">rediff</a></td><td><a href="http://www.gmail.com" onclick="javascript:return frmme(this)">gmail</a></td>
    </table>
    </BODY>
    </HTML>

    regards

  3. #3
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need a way to extract the url from the onclick function returned in the tempfunc variable.

    links[i].onclick returns the onclick unction for that td object. Anyone know of a way to et the location.href out of that function?

  4. #4
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was able to extract the url, using the code below, but when I try to re-apply it to the new onclick function at the end, every link on the page becomes the same as the last url returned. For instance, if the page returned www.url1.com, www.url2.com, www.url3.com, all the inks on the page would then go to www.url3.com. is there a way I need to number the variables in the loop so that it grabs the url associated with that loop???

    Here is the code:

    Code:
    <script type="text/javascript">
    var links = new Array();
    links = document.getElementsByTagName("td");
    for(var i=0;i<links.length;i++){
    if(links[i].onclick){
    	var tempfunc = links[i].onclick;
    	blahstring = tempfunc + "";
    	blahstring2 = blahstring.split("= \"")[1];
    	blahstring3 = blahstring2.split("\"");
    	var url = blahstring3[0] + "";
    	links[i].onclick=function(){top.location.href=url};
    }else{
    links[i].onclick = "";
    }
    }
    </script>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •