SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    San Francisco
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Get unresolved URL

    How do I get the URL that actually appears in the code of a link?

    Given a document C:/Documents and Settings/username/My Documents/test.htm

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    <!--
    function scanLinks() {
    	var aList=document.getElementsByTagName('a');
    	var aListLength = aList.length;
    	for (var count=aListLength-1;count>-1;count--) {
        	if (aList[count].href) {
            	alert(aList[count].href);
        	}
    	}
    }
    -->
    </script>
    </head>
    <body onLoad="scanLinks();">
    <a href="file:///C|/Documents and Settings/username/My Documents/test2.htm">Test 2</a>
    <a href="test3.htm">Test 3</a>
    </body>
    </html>
    Both alerts give the fully qualified path. How do I get JavaScript to give me just what is actually in the coded HTML? That is, the alert for test3.htm would contain only "test3.htm" and no path.

    Needs to work IE6+, Gecko, Opera, Safari.

  2. #2
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll need to strip the filename from the full URL. I'd probably use something like string_var.substring(lastIndexOf('/'),(string_var.length-1)), but that's just off the top of my head and, obviously, assumes Unix style pathnames.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    San Francisco
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't make myself clear. The JavaScript code will be a generic one that will be used by every page, and has no way of knowing whether there is a path to strip or not. I'm trying to detect whether there is a hard-coded path to the hard drive in the HTML, or just a relative link, when I run this script on an HTML file residing on my local machine. Therefore, the JavaScript needs to be able to read the actual HTML code, not resolved code.

    That is, I am looking for something that if the page has
    <a href="file:///C|/Documents and Settings/username/My Documents/test2.htm">Test 2</a>
    would return the string
    "file:///C|/Documents and Settings/username/My Documents/test2.htm"

    and if the page has
    <a href="test3.htm">Test 3</a>
    would return the string
    "test3.htm"

    without my having to know what is on the page.

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try element_reference.getAttribute('href');

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    San Francisco
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it! Thanks!


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
  •