• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 639
  • Last Modified:

can anchor in URL trigger javascript?

Thanks in advance on this.

Is there a way to make an anchor call in Url trigger javascript?

So you click a link to:
http://www.domain.com/anchorTest.htm#C4

And the following file alerts "hey":
<html>
<body>

<h2>Chapter 1</h2>
<p>This chapter explains ba bla bla</p>

<h2>Chapter 2</h2>
<p>This chapter explains ba bla bla</p>

<h2>Chapter 3</h2>
<p>This chapter explains ba bla bla</p>

<h2 ><a name="C4" onload="alert('hey')">Chapter 4</a></h2>
<p>This chapter explains ba bla bla</p>

</body>
</html>

0
mtnr
Asked:
mtnr
1 Solution
 
RoonaanCommented:
onclick="alert('hey');">

-r-
0
 
mtnrAuthor Commented:
it didn't say "hey" for me.  did it work for you?

the thing is, I want it to happen when the page loads, *not* when the user clicks on link.

thank you.
0
 
DreamMasterCommented:
Not gonna happen using standard anchors mtnr. You can give an onload event to a page, but it won't change depending on an anchor.

Regards,
Max.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mshogrenCommented:
I would suggest using the onload event to check the document.location object to see if it refers to the anchor like this:

<body onload="if (document.location.href.indexOf('#C4') != -1) { alert('hey'); }">
0
 
siva350Commented:
I would have to agree with mshogren
0
 
Jeremy DaleySenior DeveloperCommented:
yes... excellent answer
0
 
mtnrAuthor Commented:
very nice, this works in IE and FF.  thank you mshogen!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now