Foren / Cotonti / Support / Local header code

Hodges
#1 3. Dezember 2010, 01:24
Hey,

I've got a google map on a page set to html mode. To make it work I require this in the header:

<script type="text/javascript"> 
  function initialize() {
    var myLatlng = new google.maps.LatLng(53.381883, -1.477755);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Sir Robert Hadfield Building"
    });   
  }
</script>

And this in the body tag:

<body onLoad="initialize()">

Thing is, I don't won't this code including anywhere else on the site. How would you guys solve this elegantly?

Cheers.
pieter
#2 3. Dezember 2010, 02:04
you can give the page:
OR specific header: that page in another category and use header.CAT.tpl

OR maybe give your page an alias and add following in the standard header:

<!-- IF {PHP.al} == 'your_pagealias' -->
YOUR_CODE
<!-- ENDIF -->
... can we help you ...
GHengeveld
#3 3. Dezember 2010, 02:47
Don't use body onload, its very dirty. Also you normally shouldn't put javascript in the header, but in the footer (just before </body>). If you use html parsing in pages you can just as well put it directly in the page body, right where you want the map to be displayed. It's not the best solution, but OK if it's just one page using it. Otherwise put it in page.tpl and try this:
<!-- IF {PAGE_ID} == xx --> JS code <!-- ENDIF -->
Replace xx with the ID of the page you want the map to display on.
You can also try pieters suggestion but that requires the page to have an alias. His code will work in footer.tpl as well (probably an even better solution).

Try this:
<script type="text/javascript"> 
  function initialize() {
    var myLatlng = new google.maps.LatLng(53.381883, -1.477755);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Sir Robert Hadfield Building"
    });   
  }
  jQuery(document).ready(function(){ initialize(); });
</script>

Don't forget to remove the onload="initialize()"
pieter
#4 3. Dezember 2010, 03:08
@Koradhil
Suggestion for documentation:
List of most used tags
{PAGE_ID}
{PHP.al}
....
There are so many that nobody knows them all. Execpt programmers.
... can we help you ...
ez
#5 3. Dezember 2010, 03:25
It is not required to put JS in the header.... It is cleaner, but you can use it anywhere.

So just put your js in your page !


@Pieter:
The tags are pretty easy to find in the php files, but you are right a list should be nice..
There are some here: http://www.cotonti.com/plug.php?e=tpltags
==- I say: Keep it EZ -==
pieter
#6 3. Dezember 2010, 04:58
# ez : The tags are pretty easy to find in the php files, but you are right a list should be nice..
There are some here: http://www.cotonti.com/plug.php?e=tpltags

Some explanation next to it, would be nice
... can we help you ...
Hodges
#7 3. Dezember 2010, 22:37
Cheers everyone.

In the end I went with Koradhil's code in the page body. Works a treat.

Damn I really should learn how to use jquery now :P
Kingsley
#8 3. Dezember 2010, 22:43
# Koradhil : Also you normally shouldn't put javascript in the header, but in the footer (just before </body>).

why is that?
Hodges
#9 3. Dezember 2010, 22:52
Look here for a good answer: http://stackoverflow.com/questions/196702/where-to-place-javascript-in-a-html-file

Specifically:
Also, the reason why Yahoo EPT recommends placing JS at the bottom is because the browser must go into single-threaded mode while the JS loads and then executes. If the script is in the head or in the midst of the content, the browser will "pause" while it deals with the JS. By placing the JS at the bottom, the content will be loaded and generally visible so the user can start reading it while the browser is still dealing with the JS.
Kingsley
#10 4. Dezember 2010, 00:24
thx for the explanation hodges
foxhound
#11 6. Dezember 2010, 20:54
Well, that means you just have to look into the php files to find a variable, right?
You could even use variables which are introduced with plugins.
So, if you need something open the php files and have a look at it. I guess even a noob like me can do that ;-)



Argh, it redirected me to the wrong topic after login. Sorry.
<img src="http://www.armaholic.com/datas/thumbs/green-sea-battalion-uniforms-version-03-preview_4.jpg" alt="green-sea-battalion-uniforms-version-03-" />