alternating quotes doesn't seem to be working

D

dot

I'm trying to use the following hack to create an expanding/contracting
list of links:

<html>
<head>
<script language="Javascript" type="text/javascript">
function ChangeText(obj, originalText, newText){
if(obj.clicks == 1){
obj.innerHTML = originalText;
obj.clicks = 0;
}
else{
obj.innerHTML = newText;
obj.clicks = 1;
}}
</script>
</head>

<body>
<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'','state2 ');">Summer 2004<div
id="news1"></div>

<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'', '<a href="dir/file.html"> Link </a>

<p>');" Href example <div id=new2"></div>

</body>
</html>


The first call to ChangeText works, showing or hiding the text. The
second call with the quoted string in the href treats the double quote
after file.html as the matching quote that starts the string "ChangeText(...
I thought it was legal to embed the same kind of quote as long as you
alternate quote types (single/double). I guess not. I've tried escaping
the interior double quotes, but that doesn't seem to work either.

What am I missing?
Thanks
 
S

sancha

nothing i guess can't figure it out worse completedifferent behaviour
in mozilla and explorer :((

well you can remove the inner " u dont need it.
i used firefox it worked after removing the inner quotes but not on
explorer
:(
 
S

sancha

<html>
<head>
<script language="Javascript" type="text/javascript">
function ChangeText(obj, originalText, newText){
if(obj.clicks == 1){
obj.innerHTML = originalText;
obj.clicks = 0;
}
else{
alert(newText);
obj.innerHTML = newText;
obj.clicks = 1;
}}

</script>
</head>

<body>
<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'','state2 ');">Summer 2004<div
id="news1"></div>

<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'', '<a href=dir/file.html> Link
</a><p>');"> Href example <div id=new2"></div>

</body>
</html>
 
Z

Zifud

sancha said:
nothing i guess can't figure it out worse completedifferent behaviour
in mozilla and explorer :((

well you can remove the inner " u dont need it.
i used firefox it worked after removing the inner quotes but not on
explorer
:(

Fix the syntax errors and it 'works' in both browsers.
 
R

RobG

dot said:
I'm trying to use the following hack to create an expanding/contracting
list of links:

<html>
<head>
<script language="Javascript" type="text/javascript">

The language attribute has been depreciated for quite some time,
remove it but keep the required type attribute.
function ChangeText(obj, originalText, newText){
if(obj.clicks == 1){
obj.innerHTML = originalText;
obj.clicks = 0;
}
else{
obj.innerHTML = newText;
obj.clicks = 1;
}}
</script>
</head>

<body>
<a href="javascript:void(0);" class="news"

Don't use the javascript pseudo-protocol, just use "#"
onClick="ChangeText(news1,'','state2 ');">Summer 2004<div
id="news1"></div>

<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'', '<a href="dir/file.html"> Link </a>
<p>');" Href example <div id=new2"></div>

Once you have nested quotes, just qoute the inner ones and make sure
your HTML is valid (you have forgotten a closing '>'):

<a href="javascript:void(0);" class="news"
onClick="ChangeText(news1,'', '<a href=\'dir/file.html\'> Link </a>
</body>
</html>


The first call to ChangeText works, showing or hiding the text. The
second call with the quoted string in the href treats the double quote
after file.html as the matching quote that starts the string
"ChangeText(...
I thought it was legal to embed the same kind of quote as long as you
alternate quote types (single/double). I guess not. I've tried escaping
the interior double quotes, but that doesn't seem to work either.

What am I missing?

A cleaner way of doing what you are trying to do. Why not just code
the link, then hide/show it using either 'visibility' or 'display'?
See a simple case below:

<script type="text/javascript">
function toggle(x) {
x = document.getElementById(x);
if (x.style) {
x.style.display = (x.style.display == '')? 'none':'';
}
}
</script>

<span onClick="toggle('X');" style="cursor: pointer;">
Summer 2004
</span>
<div id="news3"></div>
<span id="X"><a href="dir/file.html"> Link </a><br></span>
<span onClick="toggle('X');" style="cursor: pointer;">
Span example
</span>
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top