ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Pass ee tags into javascript

March 24, 2010 2:52pm

Subscribe [2]
  • #1 / Mar 24, 2010 2:52pm

    escape

    20 posts

    I’ve got some data in a weblog and I want to create a simple JavaScript array in one of my templates. However I can’t seem to figure out how to get JavaScript to accept the ee tag.

    To debug this I created the following simple template to test this (which does nothing)  :ohh: .

    [removed] 
       {exp:weblog:entries weblog="myweblog" }
           alert('{my_weblog_entry}');
        {/exp:weblog:entries} 
    [removed]

    But if I just change the contents of the JavaScript alert (below) it works 😊 :

    [removed] 
       {exp:weblog:entries weblog="myweblog" }
           alert('WTF');
        {/exp:weblog:entries} 
    [removed]

    In this example the weblog entries tag loops through all my posts and displays the alert message for each weblog entry. So there is something about

    alert('{my_weblog_entry}');

    that is causing a problem.

    I’ve seen posts in this fourm that suggest inserting line breaks before/after the curly brackets or putting $conf[‘protect_javascript’] = “n”;  in the config.php file but none of these seem to work OR I don’t know how to implement them in my example.

    So what would I need to do to get

    alert('{my_weblog_entry}');

    to display my weblog content?
    Chris

  • #2 / Mar 24, 2010 3:02pm

    ender

    1644 posts

    if you view source in your browser, what is actually output by EE when you’re using the {my_weblog_entry} tag in the script block?

  • #3 / Mar 24, 2010 3:28pm

    Rob Sanchez

    335 posts

    Shot in the dark here, but have you tried having your weblog tags outside of your script tags?

    {exp:weblog:entries}
    [removed]
    alert('{my_weblog_entry}');
    [removed]
    {/exp:weblog:entries}
  • #4 / Mar 24, 2010 4:18pm

    escape

    20 posts

    Appears this form strips out the > script type=“text/javascript” < wrapper and substitutes [removed]...anyway..

    I changed the alert(’{my_weblog_entry}’); to document .writeln(’{my_weblog_entry}’); to better see what was going on inside the page. Here are the results:

    With document .writeln(”{my_weblog_entry}’); the pages sources shows this:

    document .writeln("Lorem Ipsum");
    
    document .writeln("Finibus Bonorum et Malorum");
    
    document .writeln("Nemo enim ipsam");

    The weblog entries (latin text) are being passed as expected but the paragraph elements are NOT in my weblog entry so they being inserted somewhere. Even with the elements the entry should be displayed. Seems that JavaScript is disabled.

    If I modify the JavaScript write statement to: document .writeln(‘WTF’); the text is displayed in the browser as expected.

    Go figure ❓

  • #5 / Mar 25, 2010 1:41pm

    escape

    20 posts

    This is crazy. I’ve tired numerous configurations and can’t figure out why an EE parsing tag prevents JavaScript from executing?

    What is going on?

  • #6 / Mar 25, 2010 2:02pm

    ender

    1644 posts

    if the tags are the cause of the problem, you can get EE to not add these to your text by changing the format of the custom field from ‘xhtml’ to ‘none’.

  • #7 / Mar 25, 2010 2:19pm

    escape

    20 posts

    That’s it 😉

    Changed the custom field to none and it worked.

    Thanks

  • #8 / Mar 25, 2010 3:50pm

    escape

    20 posts

    Upon closer inspection EE still seems to have a hand in manipulating one of my custom fields that contain a URL like www .mydomain.com. When EE parses the URL custom field (not just into my JavaScript, but anywhere in a template) it wraps it in an anchor tag so it comes out like:

    <a href="http://www.mydomain.com">http://www.mydomain.com</a>

    despite the fact the custom field has been set to none.

    Is there a way to override this?

    Thanks,
    Chris
    BTW: I had to put a space in my example above or EE changed it to an anchor in this post.

  • #9 / Mar 25, 2010 4:15pm

    ender

    1644 posts

    the setting you’re looking for is “Automatically turn URLs and email addresses into links?”.  This is located in Admin > Weblog Management > Edit Preferences > Weblog Posting Preferences.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases