Discussion:
Javascript backslash in php dom object
Uggla Henrik
2014-05-09 12:51:18 UTC
Permalink
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.

<?php

$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works

?>

/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Gaasbeek | Communiq
2014-05-09 13:29:47 UTC
Permalink
Adding addslashes() makes it return the script though.

<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$html = addslashes($html);
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
//echo $html;
?>

Met vriendelijke groet,

Meint-Willem Gaasbeek

Communiq BV
Walstraat 18
8011 NT Zwolle

Telefoon 038-426 00 60
Email ***@communiq.nl
Internet www.communiq.nl

De informatie verzonden met dit e-mail bericht is uitsluitend bestemd voor de geadresseerde. Gebruik van deze informatie door anderen dan de geadresseerde is verboden. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is niet toegestaan zonder voorafgaande toestemming. Communiq staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan. Tevens staat Communiq niet in voor het virusvrij zijn van een verzonden e-mail.
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-05-12 09:13:29 UTC
Permalink
Addslashes() breaks the js code:

<?php
$html = '
<html>
<head>
<style>'.file_get_contents("http://ol3js.org/en/master/css/ol.css").'</style>
<script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: "map",
renderer: "canvas",
layers: [
new ol.layer.Tile({
title: "Global Imagery",
source: new ol.source.TileWMS({
url: "http://maps.opengeo.org/geowebcache/service/wms",
params: {LAYERS: "bluemarble", VERSION: "1.1.1"}
})
})
],
view: new ol.View2D({
projection: "EPSG:4326",
center: [0, 0],
zoom: 0,
maxResolution: 0.703125
})
});
</script>
</body>
</html>';
$htmlslashes = addslashes($html);
echo $htmlslashes; //works not
//echo $html; works
?>

/H
________________________________________
Från: Gaasbeek | Communiq [***@communiq.nl]
Skickat: den 9 maj 2014 15:29
Till: Uggla Henrik; php-***@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object

Adding addslashes() makes it return the script though.

<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$html = addslashes($html);
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
//echo $html;
?>

Met vriendelijke groet,

Meint-Willem Gaasbeek

Communiq BV
Walstraat 18
8011 NT Zwolle

Telefoon 038-426 00 60
Email ***@communiq.nl
Internet www.communiq.nl

De informatie verzonden met dit e-mail bericht is uitsluitend bestemd voor de geadresseerde. Gebruik van deze informatie door anderen dan de geadresseerde is verboden. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is niet toegestaan zonder voorafgaande toestemming. Communiq staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan. Tevens staat Communiq niet in voor het virusvrij zijn van een verzonden e-mail.
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Frank Arensmeier
2014-05-09 13:30:43 UTC
Permalink
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Hi!

Try to wrap the javascript code with "<![CDATA[” like so and see if that helps:

$html = '<html>
<head>
<script type="text/javascript">
<![CDATA[’.
file_get_contents("http://ol3js.org/en/master/build/ol.js”).
']]>
</script>
</head>
<body></body>
</html>’;

/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Frank Arensmeier
2014-05-12 09:02:35 UTC
Permalink
<?php
$html = '<html><head><script type="text/javascript"><![CDATA[]]></script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
?>
/Henrik
________________________________________
Skickat: den 9 maj 2014 15:30
Till: Uggla Henrik
Ämne: Re: [PHP] Javascript backslash in php dom object
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
This seems all very backwards to me. Is there any particular reason why you want to load javascript code into the DOMDomcument prior to echoing out the HTML? Your probably won’t be able to do anything with the script element what so ever. 2Why not simply write like:

$jsurl = "http://ol3js.org/en/master/build/ol.js";
$html = <<<HTML
<html>
<head>
<script type="text/javascript" src="{$jsurl}"></script>
</head>
<body>
</body>
</html>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2;

/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Frank Arensmeier
2014-05-12 09:15:32 UTC
Permalink
I don't want each client downloading the script from http://ol3js.org separately. The script should be loaded by the server, cached and delivered to the clients.
Make a copy of that script, save it on your server and include a URL pointing to your local copy instead? What’s the difference between providing a URL and including the source code? The payload is more or less the same.
/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-05-12 09:40:42 UTC
Permalink
1. The clients should not rely on http://ol3js.org, it could be down/slow.
2. The server should always have the most recent version of the js available at http://ol3js.org.

I could do what you propose but I would then need to implement a mechanism for checking and replacing the js on my server.

/H
________________________________________
Från: Frank Arensmeier [***@gmail.com]
Skickat: den 12 maj 2014 11:15
Till: Uggla Henrik
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object
I don't want each client downloading the script from http://ol3js.org separately. The script should be loaded by the server, cached and delivered to the clients.
Make a copy of that script, save it on your server and include a URL pointing to your local copy instead? What’s the difference between providing a URL and including the source code? The payload is more or less the same.
/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Stuart Dallas
2014-05-12 11:31:22 UTC
Permalink
Post by Uggla Henrik
1. The clients should not rely on http://ol3js.org, it could be down/slow.
2. The server should always have the most recent version of the js available at http://ol3js.org.
I could do what you propose but I would then need to implement a mechanism for checking and replacing the js on my server.
Yes, you would, so do that. Your current "solution" doesn’t actually solve the problem you’re trying to address:

By having your PHP script download it from that third party site you are not preventing this issue, you’re just moving it from the client to the server. The client is still at the mercy of the speed and uptime of their server(s).

A better solution would be to have a cron job (in PHP or bash or whatever) that downloads the file from the third party to a temporary file, then move that file into your site’s filesystem. Then reference your site’s copy of that file in your script tag. Make sure you use a temporary file rather than downloading it directly into place to avoid clients trying to download while it’s being retrieved.

Run that cron job as often as you think is needed to make sure it’s always up to date. Ideally that script will make use of If-Modified-Since/Etag/whatever caching mechanism is available from the third party server to not bother actually downloading it unless it’s changed. I’d probably run the script daily, or hourly if it’s very important that it’s always the latest version (it rarely is that important).


-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-05-12 09:44:32 UTC
Permalink
I finally got it working:

<?php
$html = '
<html>
<head>
<style>'.file_get_contents("http://ol3js.org/en/master/css/ol.css").'</style>
<script type="text/javascript">'.htmlspecialchars(file_get_contents("http://ol3js.org/en/master/build/ol.js")).'</script>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: "map",
renderer: "canvas",
layers: [
new ol.layer.Tile({
title: "Global Imagery",
source: new ol.source.TileWMS({
url: "http://maps.opengeo.org/geowebcache/service/wms",
params: {LAYERS: "bluemarble", VERSION: "1.1.1"}
})
})
],
view: new ol.View2D({
projection: "EPSG:4326",
center: [0, 0],
zoom: 0,
maxResolution: 0.703125
})
});
</script>
</body>
</html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo htmlspecialchars_decode($html2);
?>

/H
________________________________________
Från: Frank Arensmeier [***@gmail.com]
Skickat: den 12 maj 2014 11:15
Till: Uggla Henrik
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object
I don't want each client downloading the script from http://ol3js.org separately. The script should be loaded by the server, cached and delivered to the clients.
Make a copy of that script, save it on your server and include a URL pointing to your local copy instead? What’s the difference between providing a URL and including the source code? The payload is more or less the same.
/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-05-12 09:11:31 UTC
Permalink
I don't want each client downloading the script from http://ol3js.org separately. The script should be loaded by the server, cached and delivered to the clients.

/H
________________________________________
Från: Frank Arensmeier [***@gmail.com]
Skickat: den 12 maj 2014 11:02
Till: Uggla Henrik
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object
<?php
$html = '<html><head><script type="text/javascript"><![CDATA[]]></script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
?>
/Henrik
________________________________________
Skickat: den 9 maj 2014 15:30
Till: Uggla Henrik
Ämne: Re: [PHP] Javascript backslash in php dom object
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
This seems all very backwards to me. Is there any particular reason why you want to load javascript code into the DOMDomcument prior to echoing out the HTML? Your probably won’t be able to do anything with the script element what so ever. 2Why not simply write like:

$jsurl = "http://ol3js.org/en/master/build/ol.js";
$html = <<<HTML
<html>
<head>
<script type="text/javascript" src="{$jsurl}"></script>
</head>
<body>
</body>
</html>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2;

/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Uggla Henrik
2014-05-12 08:44:35 UTC
Permalink
Php doesn't like <![CDATA[ either:

<?php
$html = '<html><head><script type="text/javascript"><![CDATA[]]></script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
?>

/Henrik
________________________________________
Från: Frank Arensmeier [***@gmail.com]
Skickat: den 9 maj 2014 15:30
Till: Uggla Henrik
Kopia: php-***@lists.php.net
Ämne: Re: [PHP] Javascript backslash in php dom object
Post by Uggla Henrik
I'm trying to add some js code (from Openlayers3) to a php dom object. Echoing the html with the js code directly works but not after it is loaded into the dom. I get "SyntaxError: illegal character" in Firefox. I think it's the backslashes in the js code thats the problem. How could I make it work? I've tried htmlspecialchars but it didn't work.
<?php
$html = '<html><head><script type="text/javascript">'.file_get_contents("http://ol3js.org/en/master/build/ol.js").'</script></head><body></body></html>';
$dom = new DOMDocument();
$dom->loadHTML($html);
$html2 = $dom->saveHTML();
echo $html2; //works not
// echo $html works
?>
/HU
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Hi!

Try to wrap the javascript code with "<![CDATA[” like so and see if that helps:

$html = '<html>
<head>
<script type="text/javascript">
<![CDATA[’.
file_get_contents("http://ol3js.org/en/master/build/ol.js”).
']]>
</script>
</head>
<body></body>
</html>’;

/frank
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...