Page 1 of 2
Function Within A Function
Posted: Mon Apr 24, 2006 10:54 am
by s.dot
I have a function that is called and writes some HTML to the page. Inside this HTML should be another javascript function triggered by onClick.
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '<img src="../../uploads/'+who+'/'+pic+'" alt="Photo"><p id="'+id+'"><a href="javascript: void(0);" onClick="showLink(pic,who,id);">Get Link</a></p><div id="'+id+'" style="display:none;">stuff</td>';
}
function showLink(pic,who,id){
document.getElementById(''+id+'').style.display = 'block';
}
//-->
</script>
I'm having all kinds of trouble with this one.
Posted: Mon Apr 24, 2006 11:00 am
by n00b Saibot
any specific reason you wanna do this?
Posted: Mon Apr 24, 2006 11:05 am
by s.dot
Yes. I have a photo gallery. When a thumb is clicked I want to display the full version. Then I want underneath that a link that when clicked, shows the URL to the full version.
Posted: Mon Apr 24, 2006 11:18 am
by n00b Saibot
forgive me, I didn't read it carefully. Can you tell me what problem/error message exactly do you get?
Posted: Mon Apr 24, 2006 11:24 am
by s.dot
I have altered the code a bit.
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '
<img src="../../uploads/'+who+'/'+pic+'" alt="Photo">
<p id="'+id+'"><a href="javascript: void(0);" onClick="showLink(id);">Get Link</a></p>';
}
function showLink(id){
document.getElementById(id).style.display = 'block';
}
//-->
</script>
This gives me the error "Undetermined String Constant"
Posted: Mon Apr 24, 2006 11:25 am
by feyd
using the syntax system, it becomes a bit more clear: the even will not know what pic, who and id are.
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '<img src="../../uploads/'+who+'/'+pic+'" alt="Photo"><p id="'+id+'"><a href="javascript: void(0);" onclick="showLink(\'' + pic + '\',\'' + who + '\',\'' + id + '\');">Get Link</a></p><div id="'+id+'" style="display:none;">stuff</td>';
}
function showLink(pic,who,id){
document.getElementById(id).style.display = 'block';
}
//-->
</script>
Posted: Mon Apr 24, 2006 11:26 am
by feyd
scottayy wrote:I have altered the code a bit.
This gives me the error "Undetermined String Constant"
Javascript doesn't (generally) support multiple line strings.
Posted: Mon Apr 24, 2006 11:30 am
by n00b Saibot
feyd has cleared up the cause for your error, but the additional pic and who params are not reqd.
Posted: Mon Apr 24, 2006 11:32 am
by s.dot
OK, let me try this on one line, with the syntax highlighter.
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '<img src="../../uploads/'+who+'/'+pic+'" alt="Photo"><p><a href="javascript: void(0);" onClick="showLink(id);">Get Link</a></p><div id="'+id+'" style="display: none;">stuff</div>';
}
function showLink(id){
document.getElementById(id).style.display = 'block';
}
//-->
</script>
Edit: unknown runtime error
Posted: Mon Apr 24, 2006 11:39 am
by n00b Saibot
you again made the same mistake.
onClick="showLink(id);" should be onClick="showLink('+id+');"
Posted: Mon Apr 24, 2006 11:51 am
by s.dot
OK, sorry guys, I'm just not understanding this.
The following works:
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '<img src="../../uploads/'+who+'/'+pic+'" alt="Photo"><br><br><a href="javascript: void(0);" onClick="alert('+id+');">Get Link</a>';
}
//-->
</script>
However, when I add any HTML unto the end of the function, I get an 'Unknown Runtime Error'.
Posted: Mon Apr 24, 2006 11:53 am
by feyd
Any html?
Posted: Mon Apr 24, 2006 11:54 am
by s.dot
It appears so.
Consider the above code that does work (in IE at least)
If I add <p>Stuff</p> onto the end of it, I get the runtime error.
Posted: Mon Apr 24, 2006 12:07 pm
by n00b Saibot
I don't have any problem doing whatever you mentioned... My test script being
Code: Select all
<script type="text/javascript">
<!--
function showPicDetails(pic,who,id){
document.getElementById('picView').innerHTML = '<img src="../../uploads/'+who+'/'+pic+'" alt="Photo"><br><br><a href="javascript: void(0);" onClick="alert('+id+');">Get Link</a><p>Hurray! It Runs!</p>';
}
//-->
</script>
<a href="javascript:showPicDetails('x', 'y', '1')">Click here</a><br />
<div id="picView"></div>
edit: i dunno why forum is converting : to :
pickle, note this one
edit 2: strange it converted & #058; back to : outside the syntax block. the second : above is a entity representation...
Posted: Mon Apr 24, 2006 12:15 pm
by feyd
n00b Saibot wrote:edit: i dunno why forum is converting : to :

pickle, note this one
That's not pickles problem, nor a problem with the board. It is the protection systems of the board doing that.
n00b Saibot wrote:edit 2: strange it converted & #058; back to : outside the syntax block. the second : above is a entity representation...
The board recognises entities and makes them inline.