Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript strings

Status
Not open for further replies.

hoyo

Programmer
Jan 20, 2003
4
ES
I've been getting more and more frustated with what seems to be a fairly simple problem:

I've got a javascript function that changes the innerHTML of a <div> tag. I need the content that is replaced to be a link that has an onClick function inside. This is where it gets a bit more complicated, I'm using PHP to generate this string so an added level of escaping quotes on so on needs to be considered.

MY PHP looks like this:

$string .= &quot; onChange=\&quot;MM_changeProp('locfinder','','innerHTML', 'LINK','DIV')\&quot;&quot;;

I want LINK to show
<a href=&quot;javascript:;&quot; onClick=&quot;MM_openBrWindow('location_finder.php?loc_province=' + this.value,'locfinder','scrollbars=yes,width=400,height=400')&quot;>link</a>

I can use both functions individually and they work fine.
I think I'm just getting a bit mixed up with escaping characters. Any help would be greatly appreciated!
 
hoyo,

How about loading the page in the browser. Post the source
from the browser and let's see what's actually making it to
the client. Along with the functions you are having
trouble with.

That should give us a better picture to work with.

2b||!2b
 
Here it is...

Code:
//functions kept in javascript file

function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf(&quot;style.&quot;)==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval(&quot;obj.&quot;+theProp+&quot;=&quot;+theValue);
    else eval(&quot;obj.&quot;+theProp+&quot;='&quot;+theValue+&quot;'&quot;);
  }
}


//code in html

<select name=&quot;loc_province&quot; id=&quot;loc_province&quot; size=&quot;1&quot; onChange=&quot;MM_changeProp('locfinder','<a href=\&quot;javascript:;\&quot; onClick=\&quot;MM_openBrWindow(\'location_finder.php?loc_province=' + this.value + '\',\'locfinder\',\'scrollbars=yes,width=400,height=400\')\&quot;>' + this.options[this.selectedIndex].text + '<\/a>','innerHTML', ',','DIV')&quot;>
<option value=&quot;&quot;>Any Province</option>
<option value=&quot;4&quot;>ALMERIA</option>
<option value=&quot;11&quot;>CADIZ</option>
<option value=&quot;18&quot;>GRANADA</option>
<option value=&quot;29&quot;>MALAGA</option>
</select>

<div id=&quot;locfinder&quot;> </div>
 
hoyo,

Well I would have thought you could escape &quot; also but
you can't in the case of arguments I suppose.

Anyway I was able to declare a global var qt = '&quot;';
and use that in the string. So now you don't get a string
termination error. This is what I came up with.
I hope it helps you get where you are going.

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html><head><title>TEST</title>
<script>
var qt = '&quot;';
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  alert(x);
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf(&quot;style.&quot;)==-1 || obj.style)){
    //if (theValue == true || theValue == false)
      //eval(&quot;obj.&quot;+theProp+&quot;=&quot;+theValue);
     obj.outerHTML = x;
  }
}
</script></head><body>
<select name=&quot;loc_province&quot; id=&quot;loc_province&quot; size=&quot;1&quot;
onChange=&quot;MM_changeProp('locfinder','<a href='+qt+'javascript:'+qt+' onClick='+qt+
'MM_openBrWindow(\'location_finder.php?loc_province=' + this.value + 
'\',\'locfinder\',\'scrollbars=yes,width=400,height=400\')'+qt+ '>' + this.options[this.selectedIndex].text + 
'<\/a>','outerHTML', 'TEST TXT','DIV')&quot;>
<option value=&quot;&quot;>Any Province</option>
<option value=&quot;4&quot;>ALMERIA</option>
<option value=&quot;11&quot;>CADIZ</option>
<option value=&quot;18&quot;>GRANADA</option>
<option value=&quot;29&quot;>MALAGA</option>
</select>
<div id=&quot;locfinder&quot;> </div></body></html>

2b||!2b
 
Supa! That worked a treat. Thank you very much.

:eek:) Hoyo
 
You're welcome, very enlightening working on this one.

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top