PDA

View Full Version : Adding onclick function within document.write tag



vestagirl
05-21-2008, 05:26 PM
I am trying to use a javascript that allows a series of images to randomly show, but instead of it just activating a link in an href tag, I want it to see an onclick event which contains a javascript call from another script in the page.

I am trying to put the following a href tag into a document.write tag and I can't seem to get the syntax right or know if it is possible to put another javascript function call into a different javascript:

This is the tag that would rest in a lone href tag:
<a href="#" onclick="return hs.htmlExpand(this, { contentId: 'highslide-html' } )" class="highslide"><img src="image.gif></a>

And this is how I tried to enter the tag:

document.write('<a href =\"' + url + '\" onclick=\"return hs.htmlExpand(this, { contentId: '+ contentId +' } )\" class='highslide'>');

Any help would be MUCH appreciated.

thetestingsite
05-21-2008, 05:30 PM
document.write('<a href="' + url + '" onclick="return hs.htmlExpand(this, { contentId: '+ contentId +' } )" class="highslide">');


Hope this helps.

vestagirl
05-21-2008, 10:42 PM
Thank you for the adaption, I thought I had to put an escape character in quotes. However it is ignoring my onclick tag. The function is in a script that is being called in a .js file. I even tried putting the javascript on the page instead of in an external file and it is still ignoring it.

Here is the entire document write of the javascript, am I doing something that is making it ignoring the onclick command?

document.write('<a href="' + url + '" onclick="return hs.htmlExpand(this, { contentId: '+ contentId +' } )" class="highslide">');
document.write('<img src=\"' + Graphic + '\" width=')
document.write(width + ' height=' + height + ' ');
document.write('border=0>');
document.write('</a>');

Thanks again for your help.

thetestingsite
05-21-2008, 10:48 PM
I just tried it out locally and it does work when I assign dummy variables to the code you posted above. This is the test page that I made up locally:



<html>
<head>
<title>Test</title>
</head>
<body>

<script type="text/javascript">
var url='test.html';
var Graphic='test.gif';
var width='100';
var height='100';
var contentId = 'test';


document.write('<a href="' + url + '" onclick="return hs.htmlExpand(this, { contentId: '+ contentId +' } )" class="highslide">');
document.write('<img src=\"' + Graphic + '\" width=')
document.write(width + ' height=' + height + ' ');
document.write('border=0>');
document.write('<\/a>');
</script>

</body>
</html>


If you are still having issues, please post a link to your problem page.

Hope this helps.

vestagirl
05-21-2008, 11:51 PM
Let me first say, thank you so much for helping. The image is coming up and it sees the url but it is still ignoring my onclick, the onclick should be pulling up a div with content like it does on my non java page.

Login with username: jolie@designamics.com password: test

This is the non java page with the working onclick function:

http://www.imarentertainment.com/newsite/Home.asp

Here is the java page with the images coming up and rotating but the onclick function being ignored (no div coming up):

http://www.imarentertainment.com/newsite/Home_java.asp

I have noticed that on the java page it will append the url with a # but on the non java page it will not, I don't know if the document.write is perhaps overwriting the onclick or something.

All help is appreciate immensely of course! :)

thetestingsite
05-21-2008, 11:58 PM
The error I'm getting is that hs is not defined. The reason for this is that you have not placed the following in Home_java.asp:



<script type="text/javascript" src="highslide/highslide.js"></script>


That's all it is now.

Hope this helps.

vestagirl
05-22-2008, 12:01 AM
I put the contents of the .js file right into the document in the Home_java.asp and removed the include, but I went ahead and put it back in (the call for the .js file) but it still isn't opening the div. Ideas? Again, thank you.

thetestingsite
05-22-2008, 12:07 AM
Ok, let me try explaining this again. Get rid of the actual javascript code that's in the html and only use the <script type="text/javascript"> parts to include the js. That's the only issue I see because apparently you have the hs defining script after the other part which is causing errors (if that makes sense).

Hope this helps.

vestagirl
05-22-2008, 12:22 AM
Ok I removed javascript from the actual page and have only the .js for the hs definition. That is what I had there originally, I thought maybe it wasn't pulling from the file. I have the .js script in the header and the other part in the body, but i saw you said that the hs was being defined after the other part causing errors. I tried putting the other part into a .js file too and just calling that but still no luck.

And thank you SO much for your patience and help, once again.

thetestingsite
05-22-2008, 12:24 AM
Well, now the error I'm getting is highslide not defined. Thing about that error is that I can't find any reference to it and that makes no sense. I'll keep looking to see if I can figure it out, but it might take a little while.

vestagirl
05-22-2008, 12:32 AM
I went ahead and removed the class="highslide" tag out of the document.write as it seems from my original page, is that it isn't necessary to be there to make the div appear, still doesn't work, but let me know what errors are happening now?

thetestingsite
05-22-2008, 12:34 AM
Still the same thing (which again makes no sense).

vestagirl
05-22-2008, 12:34 AM
Let me know if you happen to find anything, as I have been quite stumped for a while.

jscheuer1
05-22-2008, 02:45 PM
This is the correct way of escaping the code from the first post in this thread:


document.write('<a href ="' + url + '" onclick="return hs.htmlExpand(this, { contentId: \''+ contentId +'\' } )\" class="highslide">');

The highlighted sections show that we are passing a string value that will in turn be passed as a string value by the onclick event we are writing. This is what was missing from the original solution offered by thetestingsite.

vestagirl
05-22-2008, 05:16 PM
Changing the syntax in that line seemed to fix it, thank you both for all your help!

vestagirl
05-23-2008, 12:09 AM
Well it wasn't working, but I just went through section by section on the page it did work on and I must have fixed whatever the issue was, I think it must have just been some rogue tag. Working now :)