Page 1 of 1

[SOLVED]send news field definition JavaScript to <head>

Posted: Sun Oct 19, 2014 10:07 pm
by blackhawk
I am using CMSMS 1.11.11

If I create a news field definition (textarea), and write something like this in it...

Code: Select all

<__script__>
function test(){
/*test*/
}
</__script>
how can I send this script block to the <head> of the content page that is referencing the news article that has this block of code?

I tried capturing the eval of the field, and I tried assigning a variable to the field variable with a global scope, but no luck so far.

I basically want to write java script in my news field definition and have that sent to the head of the page.

thanks

Re: How to send news field definition JavaScript to <head>

Posted: Sun Oct 19, 2014 10:37 pm
by calguy1000
In the head of your page template:

Code: Select all

{if isset($some_funky_javascript)}
<__script__ type="text/javascript">{$some_funky_javascript}</__script>
{/if}
[/code]

In your news template:

Code: Select all

{capture assign='some_funky_javascript'}
alert('some stuff goes here');
{/capture}
Why put it inline javascript in the head though?

<__script__> tags (with a src attribute) should go either in the head or just before <__body> but why bother with putting inline javascript in the head? Just wondering.

Re: How to send news field definition JavaScript to <head>

Posted: Sun Oct 19, 2014 10:47 pm
by calguy1000
followup:

It is my opinion (till somebody gives me a good argument otherwise) that simple styles and javascript that are related to a particular piece of html/template code (AND ARE NOT RE-USED IN MULTIPLE PLACES). should be stored with that piece of html/template code. So that there is one place to edit things.

For example, if you want a confirm message displayed when a user clicks on a certain button, and/or you want to adjust the style button in a different manner than other buttons on the site...that that code is only in one spot (i.e: a news template).... and that that CSS and Javascript code should reside with the the template for easier management.

However, stylesheet and script tags that include another css or js file should either be up high in the head section, or down just before the <__body> to be loaded serially. Additionally, child requests for each page should be kept to a logical minimum (as is appropriate for the site).

but if somebody wants to show me a different argument... I am open.

Re: How to send news field definition JavaScript to <head>

Posted: Sun Oct 19, 2014 11:13 pm
by blackhawk
To answer your first question, I only have 2 reasons at the moment why I needed my field definition with its JavaScript to loaded in the head,

I am working with google maps api JavaScript v3 and they recommend ( https://developers.google.com/maps/docu ... t/tutorial) loading their code in the head of the page. I personally hate that and would rather load my JavaScript before the ending body tag. But I'm just trying to work with the system.

My second reason is that I am associating a specific news article to a specific content page. So basically my {news...} tag is on this content page. I want all the text, images and google maps api stuff to be controlled and modified from a single location - the news article page itself, rather than having some code in one page, while the rest of it is on another.

This is why I wanted to created an additional field definition (textarea), which is used for custom coding, and can be captured or assigned, and then loaded anywhere it is referenced.

But seriously, if you have any insight on how I can better my situation in this specific case, I'm all ears.

Thanks for all the help provided above calguy!

Re: How to send news field definition JavaScript to <head>

Posted: Sun Oct 19, 2014 11:16 pm
by blackhawk
I don't think I have to inline the javascript - if I reference a .js file, it would be nice to edit that same .js file in cmsms...I haven't researched this yet...getting there though...

Re: How to send news field definition JavaScript to <head>

Posted: Mon Oct 20, 2014 12:04 am
by blackhawk
Just found a answer to avoiding the inline approach...
https://www.cmscanbesimple.org/blog/eas ... s-and-code

This is actually good compromise for what I am looking for and brings a clever answer to my initial approach. I can now reference and edit my JavaScript all from within cmsms in an easy way, and the JavaScript can be stacked on the same content page, allowing me to come back to one source to make js modifications.

Thank you rolf