SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Got a small JavaScript problem. I cannot print out a JavaScript variable the way I would like. For example...

    This prints out the variable just fine:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var url = "http://www.somesite.com";
    </script>
    </head>
    
    <body>
    <script language="JavaScript">document.write(url);</script>
    </body>
    </html>
    This, however, does not:

    Code:
    <html>
    <head>
    <script language="JavaScript">
    var url = "http://www.somesite.com";
    </script>
    </head>
    
    <body>
    <a href="<script language='JavaScript'>document.write(url);</script>">
    </body>
    </html>
    As you can see, I want to be able to specify a URL with JavaScript, and then use it in a link's HREF attribute later...I cannot get it to function correctly, however. Any ideas?

  2. #2
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remember JS is processed inline with the HTML.

    Here is what you have.

    <a href = "<html tag>blah blah</html tag>">Link</a>

    Now you see why it doesn't work? You cannot include an HTML like that within the arguement on another HTML tag and hope it'll still get processed. You've been working with PHP too much.

    What you may want to try to do is this:

    var url = '<a href = "www.blah.com">link</a>'

    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I'd thought of doing it that way - was just wondering if I could do it the other way somehow. Still don't totally understand why it wouldn't work, but thank you anyway.

  4. #4
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,939
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think of it like this.

    Code:
    <a href = "<B>http:/www.blah.com</b>">Bold Link</a>
    The <script> tag is not part of javascript, its part of html. So in effect you were using an HTML tag inside another HTML tag, which is something you cannot do.

    Chris
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see. I understood the HTML within HTML part, but I guess I never thought of JavaScript that way.

    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
  •