Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

myjavascript.js:

    function initialize() {

        var waStyle = [
            {
                featureType: "all",
                stylers: [
                    { saturation: -100 }
                ]
            }
        ];

        var mysiteMap = new google.maps.StyledMapType(waStyle,{name: "Nuance"});

        var img = new google.maps.MarkerImage("/themes/nuance/img/pin.png");

        var myLatlng = new google.maps.LatLng(41.061833,28.997948);

        var mapOptions = {
            zoom: 16,
            center: myLatlng,
            disableDefaultUI: true,
            mapTypeControlOptions: {
                mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'mysite_map'],
            }
        };

        var map = new google.maps.Map(document.getElementById('mainmap'), mapOptions);

        var point = new google.maps.LatLng(41.061833,28.997948);
        var marker = new google.maps.Marker({
            position: point,
            map: map,
            animation: google.maps.Animation.DROP,
            icon: img
        })

        map.mapTypes.set('mysite_map', mysiteMap);
        map.setMapTypeId('mysite_map');
    }

    function loadScript() {
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize";
        document.body.appendChild(script);
    }

    window.onload = loadScript;

And include in my node via field. (text format: php code)

<?php include('themes/mytheme/scripts/maps/myjavascript.js'); ?>

But get error.

On site:

 function initialize() { var waStyle = [ { featureType: "all", stylers: [ { saturation: -100 } ]
    ...

What's my problem and how can i fix it?

share|improve this question

1 Answer

up vote 1 down vote accepted

The Drupal way is to add js on the go is drupal_add_js

drupal_add_js($data = NULL, $options = NULL)

Examples:

  drupal_add_js('misc/collapse.js');
  drupal_add_js('misc/collapse.js', 'file');
  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
  drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
    array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
  );
  drupal_add_js('http://example.com/example.js', 'external');
  drupal_add_js(array('myModule' => array('key' => 'value')), 'setting');

There is a better way to add a library file drupal_add_library

Parameters

$module: The name of the module that registered the library.

$name: The name of the library to add.

$every_page: Set to TRUE if this library is added to every page on the site. Only items with the every_page flag set to TRUE can participate in aggregation.

share|improve this answer
Thank you. E.G. can i include drupal_add_js('misc/collapse.js'); in my node? – Karmacoma Dec 15 '12 at 1:59
if its in core you can try drupal_add_library('system', 'ui.autocomplete'); + See my edit – Nikhil M Dec 15 '12 at 2:07
No, it's not in core, custom javascript codes. – Karmacoma Dec 15 '12 at 2:24
2  
@user1213807, try drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file.js'); – Nikhil M Dec 15 '12 at 4:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.