...

View Full Version : Script Displaying Invalid Markup



mhoram
01-24-2005, 12:55 PM
Hi all

i am a beginner regarding javascript.

i'm working with code someone else has written, its a scrolling ticker tape script.

The problem im having is that the script outputs invalid markup to the browser like so:

<a class=\"tickerfont\" onmouseover=\"this.style.color='#CC0000'\"; onmouseout=\"this.style.color='#330000'\";

as you can see there are slashes in this output, which i want to remove.

i have tried removing the slashes from where the javascript variable is set in the script like so.

$tickernews .= '<a class="tickerfont" onmouseover="this.style.color='#CC0000'"; onmouseout="this.style.color='#330000'"; href="' .$urlpiece . 'fullnews.php3?tname=' . pg_Result ($resulttick, $row, randval) . '&amp;xname=' . pg_Result ($resulttick, $row, edartist) . '">' . strip_tags(pg_Result ($resulttick, $row, edtitle)) . '</a>.........' ;

print 'var jscontent="' . $tickernews . '";';

but this results in the script not working.

i think its becuase javascript needs the slashes present to work with the variable.

So...is there anyway to strip the slashes from the string in the javascript file while maintaining the functionality of the script.

thanks in advance

Kor
01-24-2005, 01:33 PM
$tickernews .= '<a class="tickerfont" onmouseover="this.style.color=\'#CC0000\'"; ....and so on

Javascript accepts both type of quatation marks, but if the string is to be declared using one type, and if inside the string there is a need for the same type to be inserted as true string character, back slash is to be used:

var foo = '<a class="tickerfont" onmouseover="this.style.color=\'#CC0000\'">';
or
var foo="<a class=\"tickerfont\" onmouseover=\"this.style.color='#CC0000'\">";

mhoram
01-24-2005, 04:08 PM
thanks for the reply Kor

i followed your advice and only used backslashes with one type of quotation mark.

unfortunately it resulted in the ticker not working

however, when i examined the source of the page all the ticker data was there, minus the slashes that were causing the XHTML markup problem.

here is where the variable jscontent is set using php



<script language="JavaScript" type="text/javascript">
<?php

$tickernews .= '<a class=\"tickerfont\" onmouseover=\"this.style.color=\'#CC0000\'\"; onmouseout=\"this.style.color=\'#330000\'\"; href=\"' .$urlpiece . '/fullnews.php3?tname=' . pg_Result ($resulttick, $row, randval) . '&amp;xname=' . pg_Result ($resulttick, $row, edartist) . '\">' . strip_tags(pg_Result ($resulttick, $row, edtitle)) . '</a>.........' ;

print 'var jscontent="' . $tickernews . '";';
?>

</script>


heres the full code for the marquee, in case you can spot anything that might help.



//marquee width (in pixels)
var marqueewidth=570
//marquee height (in pixels, pertains only to NS)
var marqueeheight=17
//marquee scroll speed (larger is faster)
var speed=3
//marquee contents
var marqueecontents= jscontent;
/************************************************************************************
Value to preserve onload status
************************************************************************************/
var oldOnLoad;
oldOnLoad=window.onload;

if (document.all)
document.write('<div id="cmarquee01"><marquee scrollAmount='+speed+' style="width:'+marqueewidth+' ">'+marqueecontents+'</marquee></div>')

function regenerate(){
window.location.reload()
}
function regenerate2(){
if (document.layers){
setTimeout("window.onresize=regenerate",550)
intializemarquee()
}

if (oldOnLoad!=null) oldOnLoad();
}

function intializemarquee(){
document.cmarquee01.document.cmarquee02.document.write('<nobr>'+marqueecontents+'</nobr>')
document.cmarquee01.document.cmarquee02.document.close()
thelength=document.cmarquee01.document.cmarquee02.document.width
scrollit()
}

function scrollit(){
if (document.cmarquee01.document.cmarquee02.left>=thelength*(-1)){
document.cmarquee01.document.cmarquee02.left-=speed
setTimeout("scrollit()",100)
}
else{
document.cmarquee01.document.cmarquee02.left=marqueewidth
scrollit()
}
}

window.onload=regenerate2


thanks again

Kor
01-24-2005, 04:20 PM
I don't know much about php so that I presume that there might be a missunderstanding between those two languages... I don't sense why you have open a javascript code space that insert php code lines... hm

glenngv
01-25-2005, 03:37 AM
I don't know php too, but the first thing to do is to see the generated output (View-Source). Can you post the output?

mhoram
01-25-2005, 10:44 AM
yes , mixing php and javascript seems strange, again the code was already in place on the site written by someone else.

i think they are used them together as he was using php code to extract information from a database then use that data for the ticker.

here is what the output looks like in the browser.



<div>
<script language="JavaScript" type="text/javascript">
var jscontent="<a class=\"tickerfont\" onmouseover=\"this.style.color='#CC0000'\"; onmouseout=\"this.style.color='#330000'\"; href=\"http://www.bla.com\">linkcontent</a>.........<a class=\"
</script>

<script type="text/javascript" src="http://www.mcd.ie/js/marquee.js"></script>

</div>
<!-- ticker code -->
<div style="position:absolute; left:0px; top:160px; z-index:0;" onload="intializemarquee();">
</div>


then the javascript file with the script 'marquee.js' is called, then the function to start the marquee is called in the onload attribute of another <div>

Willy Duitt
01-25-2005, 11:02 AM
i followed your advice and only used backslashes with one type of quotation mark.

unfortunately it resulted in the ticker not working

however, when i examined the source of the page all the ticker data was there, minus the slashes that were causing the XHTML markup problem.


If the string is coming in from the database with the escape characters removed which is causing the javascript to break... You will need to escape the escape character to prevent the escape characters from escaping...

IE: '<a class=\\"tickerfont\\" onmouseover=....

.....Willy

glenngv
01-25-2005, 12:16 PM
here is what the output looks like in the browser.



<div>
<script language="JavaScript" type="text/javascript">
var jscontent="<a class=\"tickerfont\" onmouseover=\"this.style.color='#CC0000'\"; onmouseout=\"this.style.color='#330000'\"; href=\"http://www.bla.com\">linkcontent</a>.........<a class=\"
</script>


Is that the complete line of the output for the jscontent variable?
Of course, minus the ... inside the string. The string looks fine with \" except that there is no closing double quote that will terminate the string.


var jscontent="<a ...>linkcontent</a>.........<a class=\"";

BTW, there is no onload event handler for div tags.

mhoram
01-25-2005, 02:43 PM
thanks for all the help and feedback people

i tried your suggestion Willy Duitt, but it didnt work, despite your record amount of saying the word escape in a single sentence :)

but it inspired me, i replaced all double quotations with single quotations and it solved by invalid markup probelem

here's the code that worked.



$tickernews .= '<a class=\'tickerfont\' onmouseover=\'this.style.color=#CC0000\' onmouseout=\'this.style.color=#330000\' href=\'' .$urlpiece . '/fullnews.php3?tname=' . pg_Result ($resulttick, $row, randval) . '&amp;xname=' . pg_Result ($resulttick, $row, edartist) . '\'>' . strip_tags(pg_Result ($resulttick, $row, edtitle)) . '</a>.........';


thanks glenngv i just pasted an incomplete string as an example..my fault.

but yes, your of course right about the onload handler for div tags.

so i put it in the <body> tags



<body onload="intializemarquee();">


this gave me more problems as there are <a> tags in the ticker string. and it was called outside the <body> tags, which also is not valid markup.

so basically i have just ditched the links in the ticker string, so it just scrolls as headlines rather than links.

so the final string looks like this



$tickernews .= strip_tags(pg_Result ($resulttick, $row, edtitle)).'.........';


so probably didnt need to worry about the quote problem at all

not to worry....it was an interesting process

thanks for all your help again everyone.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum