Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mitra 81 posts 196 karma points
    Nov 29, 2013 @ 10:53
    Mitra
    0

    How to pass a message to a javascript

    I have got a javascript in the Scripts folder, I was kind of hopping to pass a variable to the javascrip. is it possible?

    I call my script in my masterpage like this:

    <script src="/Scripts/cookie.js" type="text/javascript">

    and then i want to call the variable like this:

    message = message + ""<umbraco:Item field="cookietextMessage" runat="server" />"<br/>";

     

    the cookietextMessage is in the document type.

    anybody knows how to do this?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 29, 2013 @ 11:06
    Dennis Aaen
    0

    Hi Mitra,

    Maybe this blog post can help you solve your problem.

    http://unencode.blogspot.dk/2012/08/umbracoitem-to-javascript.html

    /Dennis

  • Mitra 81 posts 196 karma points
    Nov 29, 2013 @ 11:12
    Mitra
    0

    But can i use it like this?

    <script type="text/javascript" src="/Scripts/cookie.js">  

    var textMessage= "<Umbraco:Item field="cookietextMessage" runat="server" />"

    script>

     

    when i use it like this the script doesn't recognice textMessage

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Nov 29, 2013 @ 11:46
    Tim
    1

    Hi Mitra,

    The javascript file is NOT parsed by the .Net page renderer, so including the umbraco:item in the script file won't work.

    It WILL work if you include it on your master page (I'm assuming you're using web forms rather than MVC). So if you add:

    <script type="text/javascript" src="/Scripts/cookie.js">
    <script type="text/javascript">
    var textMessage = '<Umbraco:Item field="cookietextMessage" runat="server" />';
    </script>
    

    That should work. If you're using MVC rather than web forms, swap the Umbraco.Item code for @Umbraco.Item("cookieTextMessage") instead.

    Hope that helps!

  • Mitra 81 posts 196 karma points
    Nov 29, 2013 @ 12:09
    Mitra
    0

    It does not work.I think because the variable is not defined in the actual javascript, so the javascript does not read it.

    It's ok though, I'm going to put it in a usercontrol.

     

    Thanks a lot anyway :)

Please Sign in or register to post replies

Write your reply to:

Draft