Jump to:

23456 Posts in 18905 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » How to add JavaScript into the content of page

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2101 Views
  • demmy
    Avatar
    Community Member
    6 Posts

    How to add JavaScript into the content of page Link to this post

    Dear All,

    I am very new in silverstripe, i start from the tutorial template and just finish to make some layout in the front page, now I try to add some javascript into the content of the main page.

    example: add javascript of Google Maps API3, where should I add? page.ss or page.php??

    Thanks.
    Demmy

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: How to add JavaScript into the content of page Link to this post

    Hi,
    My solution is probably not the best, but I usually :
    - put the requirement to the googleapi js file in my page.php controller

    Requirements::javascript('http://maps.googleapis.com/maps/api/js?sensor=false');


    - put the rest of js code in the template. It enables me to insert variables directly in the js code, so that the user can change parameters in the cms (lat/long, zoom factor, etc.).

    <script type="text/javascript">function initialize() {...


    Stan

  • Nivanka
    Avatar
    Community Member
    395 Posts

    Re: How to add JavaScript into the content of page Link to this post

    sometimes if you want to insert any JS in the head of the document, you can use Requirements::insertHeadTag function this is very useful when you want to include modernizr js etc.

  • demmy
    Avatar
    Community Member
    6 Posts

    Re: How to add JavaScript into the content of page Link to this post

    Dear Stan,

    Thanks, but i'm still not clear about this point,

    - put the rest of js code in the template. It enables me to insert variables directly in the js code, so that the user can change parameters in the cms (lat/long, zoom factor, etc.).

    can you explain a bit clearer than this?

    Best Regards;
    BOly

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: How to add JavaScript into the content of page Link to this post

    @Demmy, sorry if I wasn't clear. I give you an exemple : I wanted my client to be able to change the position of the "coloured pin" on his Google Map.

    I added a DB field named "GPS" in my ContactPage.php (ContactPage.ss is the template where I display the map) :

    <?php
    class PageContact extends Page {
    public static $db = array(
    'GPS' => 'Text'
    );
    ...
    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.GoogleMap', new TextField('GPS', 'Coordonnées GPS (format : 47.338979,1.00158)')); return $fields;
    }

    And in ContactPage.ss :

    <% if GPS %>
    <script type="text/javascript">
    function initialize() {
    var latlng = new google.maps.LatLng(
    {$GPS});
    var myOptions = {
    zoom: 15,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("GMap"),
    myOptions);
    var marker = new google.maps.Marker({
    position: latlng,
    map: map,
    title:"$SiteConfig.Title",
    visible:true
    });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    <div id="GMap">
    </div>
    <% end_if %>

    The map displayed in the site is centered on the position entered by the client in the CMS.

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: How to add JavaScript into the content of page Link to this post

    @Nivanka - Thanks, I didn't know there was a way to choose scripts that have to be in the <head> section, while the others remain at the end of the document !

  • demmy
    Avatar
    Community Member
    6 Posts

    Re: How to add JavaScript into the content of page Link to this post

    Dear Stan,

    Many thanks. Many thanks.

    I try it now, and i did not see any error, but i cannot see the Google map display on my page, its just blank screen.

    do you know what is the problem?

    Best Regards;
    Boly

    PS: If you don't mind, can you give me an email address? and can I ask you directly by email?

  • stallain
    Avatar
    Community Member
    61 Posts

    Re: How to add JavaScript into the content of page Link to this post

    If possible, can you give me the URL of your project ?
    My e-mail : stallain@gmail.com

    2101 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.