Function Within A Function

JavaScript and client side scripting.

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Function Within A Function

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

any specific reason you wanna do this?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

forgive me, I didn't read it carefully. Can you tell me what problem/error message exactly do you get?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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"
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

feyd has cleared up the cause for your error, but the additional pic and who params are not reqd.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you again made the same mistake.
onClick="showLink(id);" should be onClick="showLink('+id+');"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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'.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Any html?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

I don't have any problem doing whatever you mentioned... My test script being :arrow:

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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply