Skip to main content
gamedev.net
🔒 Locked

[web] Javascript errors?

Started by MatsK 3 replies 2.3k views
Original Post
MatsK
MatsK
I'm trying to embed Javascript in a PHP document. At first I tried to have Javascript outputted to the browser using PHP's echo(), but it didn't seem to work.
Then I tried just using raw Javascript in combination with PHP, and it gave me a grey Flash window. That's pretty much where I'm stuck right now. The Javascript code was generated by a tool called AxMag. It works well enough on its own, but not when I try to embed it!

<?php
$Magazine = $_GET['Magazine'];

echo("<html>");
echo("<!-- Created on:03/17/2011 02:34:19 pm-->");
echo("<head>");
echo(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
echo(" <title>" . $Magazine . "</title>");
echo(" <meta name=\"description\" content=\"\">");
echo(" <meta name=\"keywords\" content=\"bolig boliger boligmagasinet grafisk partner agder kristiansand evje hornes\">");
echo(" <meta name=\"author\" content=\"Mats Vederhus\">");
echo(" <meta name=\"generator\" content=\"AceHTML Freeware\">");
echo(" <img src=\"Boligmagasinet - headding.jpg\">");
echo(" <link rel=stylesheet href=\"mags-style.css\">");
echo("</head>");
echo("<body>");
echo(" <div class=\"hovermenu\">");
echo(" <ul>");
echo(" <li><a href=\"index.php\">Hjem</a></li>");
echo(" <li><a href=\"about.html\">Om oss</a></li>");
echo(" <li><a href=\"contact.html\">Kontakt</a></li>");
echo(" <li><a href=\"mags.php\">Magasiner</a></li>");
echo(" <li><a href=\"login.php\">Login</a></li>");
echo(" </ul>");
echo(" </div>");

/*echo(" <script language=\"JavaScript\"");
echo(" function getURLParam(strParamName){");
echo(" var strReturn = \"\";");
echo(" var strHref = window.location.href;");
echo(" if ( strHref.indexOf(\"?\") > -1 ){");
echo(" var strQueryString = strHref.substr(strHref.indexOf(\"?\")).toLowerCase();");
echo(" var aQueryString = strQueryString.split(\"&\");");
echo(" for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){");
echo(" if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + \"=\") > -1 ){");
echo(" var aParam = aQueryString[iParam].split(\"=\");");
echo(" strReturn = aParam[1];");
echo(" break;");
echo(" }");
echo(" }");
echo(" }");
echo(" return unescape(strReturn);");
echo(" }");

echo(" document.write(");
echo(" '<OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"\n'+");
echo(" ' codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\"\n'+");
echo(" ' WIDTH=\"100%\" HEIGHT=\"100%\" id=\"idswf\" align=\"TL\">\n'+");
echo(" ' <PARAM NAME=\"allowScriptAccess\" value=\"sameDomain\" />\n'+");
echo(" ' <PARAM NAME=\"allowFullScreen\" value=\"false\" />\n'+");
echo(" ' <PARAM NAME=movie '+");
echo(" ' VALUE=\"" . "tmpsites/" . $Magazine . "/main.swf?page='+getURLParam('page')+'\" />\n'+");
echo(" ' <PARAM NAME=quality VALUE=high />\n'+");
echo(" ' <PARAM NAME=bgcolor VALUE=#888888 />' +");
echo(" ' <PARAM NAME=scale VALUE=noscale />\n'+");
echo(" ' <PARAM NAME=align VALUE=TL />\n'+");
echo(" ' <PARAM NAME=salign VALUE=TL />\n'+");
echo(" ' <EMBED src=\"" . "tmpsites/" . $Magazine . "/main.swf?page=' + getURLParam('page') + '\"' +");
echo(" ' bgcolor=#888888 WIDTH=\"100%\" HEIGHT=\"100%\" '+");
echo(" ' quality=\"high\"' +");
echo(" ' scale=\"noscale\"' +");
echo(" ' name=\"nameswf\"' +");
echo(" ' align=\"TL\"' +");
echo(" ' salign=\"TL\"' +");
echo(" ' allowFullScreen=\"false\"' +");
echo(" ' TYPE=\"application/x-shockwave-flash\"'+");
echo(" ' />\n'+");
echo(" '</OBJECT>\n');");
echo(" </script>");*/
?>

<script language="JavaScript">
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return unescape(strReturn);
}

var Magazine = getURLParam('Magazine');

document.write(
'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n'+
' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"\n'+
' WIDTH="100%" HEIGHT="100%" id="idswf" align="TL">\n'+
' <PARAM NAME="allowScriptAccess" value="always" />\n'+
' <PARAM NAME="allowFullScreen" value="true" />\n'+
' <PARAM NAME=movie '+
' VALUE="tmpsites/'+Magazine+'/main.swf?page='+getURLParam('page')+'" />\n'+
' <PARAM NAME=quality VALUE=high />\n'+
' <PARAM NAME=bgcolor VALUE=#888888 />' +
' <PARAM NAME=scale VALUE=noscale />\n'+
' <PARAM NAME=align VALUE=TL />\n'+
' <PARAM NAME=salign VALUE=TL />\n'+
' <EMBED src="tmpsites/'+Magazine+'/main.swf?page=' + getURLParam('page') + '"' +
' bgcolor=#888888 WIDTH="100%" HEIGHT="100%" '+
' quality="high"' +
' scale="noscale"' +
' name="nameswf"' +
' align="TL"' +
' salign="TL"' +
' allowFullScreen="true"' +
' TYPE="application/x-shockwave-flash"'+
' />\n'+
'</OBJECT>\n');
</script>

<?php
echo("</body>\n");
echo("<footer>\n");
echo(" <b>Made by Mats Vederhus for Grafisk Partner avdeling Agder</b>\n");
echo("</footer>\n");
echo("</html>");
?>



My directory structure is as follows:

Boligmagasinet
Boligmagasinet/tmpsites/
Boligmagasinet/tmpsites/Boligmagasinet nr.1 - 2011

I changed the following line:

' <PARAM NAME="allowScriptAccess" value="always" />\n'+

from "sameDomain" to "always" because this article seemed to suggest it might be a good idea. It didn't seem to change anything though.
I also tried calling the php file with a url ending in '&page=1', but it didn't seem to change anything, either!

The original 'index.html' file generated by AxMag is as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>Boligmagasinet nr 1 - 2011 - Powered by aXmag</title>
</head>
<body bgcolor="#888888" style="margin:0px" >
<!-- saved from url=(0013)about:internet -->
<script language="JavaScript" type="text/javascript">
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return unescape(strReturn);
}

document.write(
'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n'+
' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"\n'+
' WIDTH="100%" HEIGHT="100%" id="idswf" align="TL">\n'+
' <PARAM NAME="allowScriptAccess" value="sameDomain" />\n'+
' <PARAM NAME="allowFullScreen" value="true" />\n'+
' <PARAM NAME=movie '+
' VALUE="main.swf?page='+getURLParam('page')+'" />\n'+
' <PARAM NAME=quality VALUE=high />\n'+
' <PARAM NAME=bgcolor VALUE=#888888 />' +
' <PARAM NAME=scale VALUE=noscale />\n'+
' <PARAM NAME=align VALUE=TL />\n'+
' <PARAM NAME=salign VALUE=TL />\n'+
' <EMBED src="main.swf?page=' + getURLParam('page') + '"' +
' bgcolor=#888888 WIDTH="100%" HEIGHT="100%" '+
' quality="high"' +
' scale="noscale"' +
' name="nameswf"' +
' align="TL"' +
' salign="TL"' +
' allowFullScreen="true"' +
' TYPE="application/x-shockwave-flash"'+
' />\n'+
'</OBJECT>\n');
</script>
</body>


Any suggestions?

Edit: Is there any sane, reasonable way to debug Javascript? It seems to basically contain an error (since the flash animation isn't showing up), but I'm not sure where it is...
AdrianC
AdrianC
I didn't look at your code that much so maybe I missed it, but why are you trying to echo the javascript? Is there a value in there that is influenced by php?
Dino
Dino
My recommendation is to move all your JS code to a separate file and only have JS code that is dependent on the current rendering state being outputted via PHP. Otherwise you are going to go crazy trying to figure out where you missed a quote.

With regards to debugging, install FireBug in FireFox to see the output of your PHP code. It will be much easier to track that way.

Finally, try putting together a page that has just static HTML in it with your <OBJECT /> tag defined in there. I really don't see a good reason to have JavaScript put that in there as a document.write. You are going through all that for a dynamic value from the query string? You could do that in just PHP quite easily. I don't do PHP, but I think it's something along the lines of:
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100%" height="100%" id="idswf" align="TL">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="true" />
<param name="movie" value="main.swf?page=<?php echo $_GET['page']; ?>" />
<param name="quality" value="high" />
<param name="bgcolor" value="#888888" />
<param name="scale" value="noscale" />
<param name="align" value="TL" />
<param name="salign" value="TL" />
<embed src="main.swf?page=<?php echo $_GET['page']; ?>" bgcolor="#888888" width="100%" height="100%" quality="high" scale="noscale" name="nameswf" align="TL" salign="TL" allowfullscreen="true" type="application/x-shockwave-flash" />
</object>
Dino M. Gambone
Good judgment is gained through experience. Experience, however, is gained through bad judgment. Currently working on Rise of Praxis MUD: http://www.riseofpraxis.net/

 
MatsK
MatsK
I fixed it.

This is the final code:

<?php
$Magazine = $_GET['Magazine'];

//Copied from 'mags.php'.
//This extracts the contents of a zip file.
function UnzipFlashContents($Filename)
{
$ZipHandle = zip_open($Filename);

if($ZipHandle)
{
while($ZipEntry = zip_read($ZipHandle))
{
$Buf = zip_entry_read($ZipEntry, zip_entry_filesize($ZipEntry));

//Javascript is (apparently) case-sensitive... (see generatemags.php).
@mkdir("tmpsites/" . strtolower(basename($Filename, ".zip")));
@mkdir("tmpsites/" . strtolower(basename($Filename, ".zip")) . "/Files");

$FileHandle = fopen("tmpsites/" . strtolower(basename($Filename, ".zip")) . '/' . zip_entry_name($ZipEntry), "w");
fwrite($FileHandle, $Buf);
fclose($FileHandle);
}
}
//TODO: Zip error?
}

//If the magazine requested wasn't extracted, extract it!
if(!file_exists('tmpsites/' . strtolower($Magazine)))
UnzipFlashContents('PDFs/' . $Magazine . '.zip');

echo("<html>");
echo("<!-- Created on:03/17/2011 02:34:19 pm-->");
echo("<head>");
echo(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
echo(" <title>" . $Magazine . "</title>");
echo(" <meta name=\"description\" content=\"\">");
echo(" <meta name=\"keywords\" content=\"bolig boliger boligmagasinet grafisk partner agder kristiansand evje hornes\">");
echo(" <meta name=\"author\" content=\"Mats Vederhus\">");
echo(" <meta name=\"generator\" content=\"AceHTML Freeware\">");
echo(" <img src=\"Boligmagasinet - headding_gray.jpg\">");
echo(" <link rel=stylesheet href=\"mags-style.css\">");
echo("</head>");
echo("<body>");
echo(" <div class=\"hovermenu\">");
echo(" <ul>");
echo(" <li><a href=\"index.php\">Hjem</a></li>");
echo(" <li><a href=\"about.html\">Om oss</a></li>");
echo(" <li><a href=\"contact.html\">Kontakt</a></li>");
echo(" <li><a href=\"mags.php\">Magasiner</a></li>");
echo(" <li><a href=\"login.php\">Login</a></li>");
echo(" <li><a href=\"displaypriceinfo.php?PDF=2011 Boligmagasinet utgivelser 2011 - materiellfrist - til trykk - utgivelse.pdf\">Priser</a></li>\n");
echo(" </ul>");
echo(" </div>");
?>

<script type="text/javascript">
function getURLParam(strParamName){
var strReturn = "";
var strHref = window.location.href;
if ( strHref.indexOf("?") > -1 ){
var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
var aQueryString = strQueryString.split("&");
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
if ( aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
var aParam = aQueryString[iParam].split("=");
strReturn = aParam[1];
break;
}
}
}
return unescape(strReturn);
}

var Magazine = getURLParam('Magazine');

document.write(
'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n'+
' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"\n'+
' WIDTH="100%" HEIGHT="67%" id="idswf" align="TL">\n'+
' <PARAM NAME="allowScriptAccess" value="always" />\n'+
' <PARAM NAME="allowFullScreen" value="true" />\n'+
' <PARAM NAME=movie '+
' VALUE="http://www.boligmagasinet.no/tmpsites/'+Magazine+'/main.swf?page='+getURLParam('page')+'" />\n'+
' <PARAM NAME=quality VALUE=high />\n'+
' <PARAM NAME=bgcolor VALUE=#888888 />' +
' <PARAM NAME=scale VALUE=noscale />\n'+
' <PARAM NAME=align VALUE=TL />\n'+
' <PARAM NAME=salign VALUE=TL />\n'+
' <EMBED src="http://www.boligmagasinet.no/tmpsites/'+Magazine+'/main.swf?page=' + getURLParam('page') + '"' +
' bgcolor=#888888 WIDTH="100%" HEIGHT="67%" '+
' quality="high"' +
' scale="noscale"' +
' name="nameswf"' +
' align="TL"' +
' salign="TL"' +
' allowFullScreen="true"' +
' TYPE="application/x-shockwave-flash"'+
' />\n'+
'</OBJECT>\n');
</script>

<?php
echo("</body>\n");
echo("<footer>\n");
echo(" <pre> <font size=\"2\" face=\"system\"><b>Laget av Mats Vederhus for Grafisk Partner</b></font></pre>");
echo("</footer>\n");
echo("</html>");
?>



Thanks guys!
Dino
Dino
[Note: I'm not a PHP developer so what I post as PHP code may not be correct and I have not tested it]

Congratulations! I would love to take credit, but I have no idea how you fixed it. One thing that I do know (or at least have an opinion on) is that your PHP is going to cause a great deal of confusion and make for debugging nightmare because you are outputting most of your HTML via echo.

Try this instead (or something like it that works):
<?php
$Magazine = $_GET['Magazine'];
$page = $_GET['page'];

//Copied from 'mags.php'.
//This extracts the contents of a zip file.
function UnzipFlashContents($Filename)
{
$ZipHandle = zip_open($Filename);

if($ZipHandle)
{
while($ZipEntry = zip_read($ZipHandle))
{
$Buf = zip_entry_read($ZipEntry, zip_entry_filesize($ZipEntry));

//Javascript is (apparently) case-sensitive... (see generatemags.php).
@mkdir("tmpsites/" . strtolower(basename($Filename, ".zip")));
@mkdir("tmpsites/" . strtolower(basename($Filename, ".zip")) . "/Files

$FileHandle = fopen("tmpsites/" . strtolower(basename($Filename, ".zip")) . '/' . zip_entry_name($ZipEntry), "w
fwrite($FileHandle, $Buf);
fclose($FileHandle);
}
}
//TODO: Zip error?
}

//If the magazine requested wasn't extracted, extract it!
if(!file_exists('tmpsites/' . strtolower($Magazine)))
UnzipFlashContents('PDFs/' . $Magazine . '.zip');
?>

<html>
<!-- Created on:03/17/2011 02:34:19 pm-->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title /><?php echo($Magazine); ?></title>
<meta name="description" content="">
<meta name="keywords" content="bolig boliger boligmagasinet grafisk partner agder kristiansand evje hornes">
<meta name="author" content="Mats Vederhus">
<meta name="generator" content="AceHTML Freeware">
<img src="Boligmagasinet - headding_gray.jpg">
<link rel=stylesheet href="mags-style.css">
</head>
<body>
<div class="hovermenu">
<ul>
<li><a href="index.php">Hjem</a></li>
<li><a href="about.html">Om oss</a></li>
<li><a href="contact.html">Kontakt</a></li>
<li><a href="mags.php">Magasiner</a></li>
<li><a href="login.php">Login</a></li>
<li><a href="displaypriceinfo.php?PDF=2011 Boligmagasinet utgivelser 2011 - materiellfrist - til trykk - utgivelse.pdf">Priser</a></li>\n
</ul>
</div>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100%" height="67%" id="idswf" align="TL">
<param name="allowScriptAccess" value="always" />
<param name="allowFullScreen" value="true" />
<param name="movie" value="http://www.boligmagasinet.no/tmpsites/<?php echo($Magazine); ?>/main.swf?page=<?php echo($page); ?>" />
<param name="quality" value="high" />
<param name="bgcolor" value="#888888" />
<param name="scale" value="noscale" />
<param name="align" value="TL" />
<param name="salign" value="TL" />
<embed src="http://www.boligmagasinet.no/tmpsites/<?php echo($Magazine); ?>/main.swf?page=<?php echo($page); ?>"'" bgcolor="#888888" width="100%" height="67%" quality="high" scale="noscale" name="nameswf" align="TL" salign="TL" allowfullscreen="true" type="application/x-shockwave-flash" />
</object>
</script>

</body>
<footer>
<pre> <font size="2" face="system"><b>Laget av Mats Vederhus for Grafisk Partner</b></font></pre>
</footer>
</html>


The idea is to not have JavaScript output HTML unless you completely have to. Also, the vast majority of the code is simple HTML. Remember KISS: Keep it Simple Stupid (no offense intended, it's just a saying).
Dino M. Gambone
Good judgment is gained through experience. Experience, however, is gained through bad judgment. Currently working on Rise of Praxis MUD: http://www.riseofpraxis.net/

 

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.