Basic Javascript DOM question that eludes me...

F

Fred!head

Hi,

I've banged my head for an hour researching and trying to figure out
what must be a simple thing in Javascript. If someone could help me or
point me in the right direction, I'd be grateful.

I want to add a link/image on 100 plus pages. Each link has to pass
the current page URL and page title contained in an H2 tag on the page
to a URL that'll process those values.

What I need is to output a URL like this:

http://www.mydomain.com/filename.php?u=CurrentPageURL&t=CurrentPageTitle

Sounds easy but I'm stuck on how to pull the value from the H2 tag in
the page. I've tried adding an id="header" value then calling it with:

document.getElementById('header')

However, I get 'null' as the screen output. Ideally I'd like to just
grab the value of the text between my <h2></h2> tags because it is
more efficient (less code).

I'm also able to grab the current URL with this javascript:

document.URL

Finally, ideally I want to pass this value to link dynamically so when
a user clicks the link it'll take them to the page. Can this be done
by passing a value to the href= value, like this:

var val = url;
loc = document.getElementById('link'); loc.href=val;

where the link is tagged id="link"? Or should I just have the
javascript output using document.write and then call the javascript
function inline?

Any ideas are appreciated. Thank you.

Tim
 
B

Bart Van der Donck

Fred!head said:
I've banged my head for an hour researching and trying to figure out
what must be a simple thing in Javascript. If someone could help me or
point me in the right direction, I'd be grateful.

I want to add a link/image on 100 plus pages. Each link has to pass
the current page URL and page title contained in an H2 tag on the page
to a URL that'll process those values.

What I need is to output a URL like this:

   http://www.mydomain.com/filename.php?u=CurrentPageURL&t=CurrentPageTitle

Sounds easy but I'm stuck on how to pull the value from the H2 tag in
the page. I've tried adding an id="header" value then calling it with:

    document.getElementById('header')

However, I get 'null' as the screen output. Ideally I'd like to just
grab the value of the text between my <h2></h2> tags because it is
more efficient (less code).

I'm also able to grab the current URL with this javascript:

    document.URL

Finally, ideally I want to pass this value to link dynamically so when
a user clicks the link it'll take them to the page. Can this be done
by passing a value to the href= value, like this:

    var val = url;
    loc = document.getElementById('link'); loc.href=val;

where the link is tagged id="link"? Or should I just have the
javascript output using document.write and then call the javascript
function inline?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<html>
<head>
<title>Dynamically compile link</title>
</head>
<body>
<h2>My title</h2>
<p>Web page content...</p>
<p>
<a href="javascript:;" onClick="
var h2_title = document.getElementsByTagName('h2')[0].innerHTML;
self.location.href =
'http://www.mydomain.com/filename.php?u='
+ encodeURIComponent(self.location.href)
+ '&amp;t='
+ encodeURIComponent(h2_title);
">Link</a>
</p>
</body>
</html>

I'm assuming that the <h2> where the title is in, is the first <h2> of
the document.

Note that & must be encoded as &amp; in the URL:
http://www.htmlhelp.com/tools/validator/problems.html#amp

Maybe some would still prefer escape() in stead of
encodeURIComponent(); that also depends on how filename.php is
configured to decode the query-string.

Hope this helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top