...

View Full Version : Output Javascript with PHP



sted
11-19-2011, 02:13 AM
Hello,

I am currently trying to implement the Nokia Maps API in my website and I am having trouble dynamically outputting javascript with PHP.

Assuming that I am correct that when a browser opens a website the browser parses the html string and implements the code, including javascript scripts.
So theoretically it doesen't matter if I hardcode it in the HTML or if I "echo" it in PHP!?

This is the hardcoded html:

<div id="map"></div>
<script type="text/javascript">
nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
var map = new nokia.maps.map.Display(
document.getElementById("map"), {
components: [
// Behavior collection
new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar() ],
zoomLevel: 10,
center: [52.51, 13.4]
});

</script>


and this is the php code to echo the exact same code:

echo '<div id="map"></div>
<script type="text/javascript">
nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
var map = new nokia.maps.map.Display(
document.getElementById("map"), {
components: [
// Behavior collection
new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar() ],
zoomLevel: 10,
center: [52.51, 13.4]
});
map.removeComponent(map.getComponentById("zoom.MouseWheel"));
</script>';

This is the actual HTML Output of the php script:

<div id="map"></div>
<script type="text/javascript">
nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
var map = new nokia.maps.map.Display(
document.getElementById("map"), {
components: [
// Behavior collection
new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar() ],
zoomLevel: 10,
center: [52.51, 13.4]
});
map.removeComponent(map.getComponentById("zoom.MouseWheel"));
</script>

Adding
document.write("Hello World");
to the script displayed the text "Hello World", so why isn't the rest executing?

Thanks in advance

loki421
11-19-2011, 03:48 PM
I sometimes find when creating js in php any line breaks get added too, and this causes a js error.

Try having the php string all on one line and give that a shot, might help?

tangoforce
11-19-2011, 06:23 PM
Use a heredoc instead:


print <<<STOP
<div id="map"></div>
<script type="text/javascript">
nokia.maps.util.ApplicationContext.set({"appId": "*****", "authenticationToken": "*****"});
var map = new nokia.maps.map.Display(
document.getElementById("map"), {
components: [
// Behavior collection
new nokia.maps.map.component.Behavior(),
new nokia.maps.map.component.ZoomBar(),
new nokia.maps.map.component.Overview(),
new nokia.maps.map.component.TypeSelector(),
new nokia.maps.map.component.ScaleBar() ],
zoomLevel: 10,
center: [52.51, 13.4]
});
map.removeComponent(map.getComponentById("zoom.MouseWheel"));
</script>
STOP;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum