How to add javascript to <head> tag?

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
param2112
New Member
New Member
Posts: 3
Joined: Mon Aug 12, 2013 7:30 am

How to add javascript to <head> tag?

Post by param2112 »

I want to install a small mobile redirect script in the <HEAD>.
How do I do that using CMSMS?
faglork

Re: How to add javascript to <head> tag?

Post by faglork »

Hi!

Depends.

If you need it on all pages:
- Global Metadata
- template(s)
- footer GCB (if you can load it at the end of the page)

If you need it on few pages/one page: the page's metadata

hth,
Alex
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: How to add javascript to <head> tag?

Post by velden »

Just add the script to the head section in the template(s) I guess (Layout -> Templates)

When it's a script file you need to include you can upload that to for example the /uploads directory and include

Code: Select all

<__script__ type="text/javascript" src="{uploads_url}/example.js"></__script>
When you write javascript in a template or content make sure to use {literal}...javscript code here...{/literal} OR make sure all '{' are surrounded by at least one space. Else the Smarty engine will try and fail to interpret the tags.

As faglork suggest, Global Metadata can be used too (Site Admin -> Global Settings)
param2112
New Member
New Member
Posts: 3
Joined: Mon Aug 12, 2013 7:30 am

Re: How to add javascript to <head> tag?

Post by param2112 »

I want to add the redirect script only on the home page.
This is a small mobile redirect script which will redirect mobile user to mobile website.

I went to pages > Home Page > options.
Typed in the redirect script in "Page Specific Metadata:" section. clicked apply and refreshed the home page.
It gave me error message on home page.

Here is example of redirect script.

<!--Simple Mobile Redirect-->
<__script__ type="text/javascript">
if(window.location.hash != '#r=off') {
if (screen.width <= 1023) {
document.location = "http://m.google.com";
}
}
</__script>

And another thing.
I do not see site admin option when I log in to CMSMS.

Any ideas?
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: How to add javascript to <head> tag?

Post by velden »

It gave me error message on home page.
Please post at least the error message!

Try:

Code: Select all

<!--Simple Mobile Redirect-->
<__script__ type="text/javascript">
{literal}
if(window.location.hash != '#r=off') {
if (screen.width <= 1023) {
document.location = "http://m.google.com";
}
}
{/literal}
</__script>
And another thing.
I do not see site admin option when I log in to CMSMS.
Then you're probably not using an admin account to log in to cmsms.
Or maybe it's an older version you did not post any information about your system
param2112
New Member
New Member
Posts: 3
Joined: Mon Aug 12, 2013 7:30 am

Re: How to add javascript to <head> tag?

Post by param2112 »

HI,

Thanks for your help.

<!--Simple Mobile Redirect-->
<__script__ type="text/javascript">
{literal}
if(window.location.hash != '#r=off') {
if (screen.width <= 1023) {
document.location = "http://m.google.com";
}
}
{/literal}
</__script>


The code above has worked.
It is working now.

Thanks a lot.
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: How to add javascript to <head> tag?

Post by psy »

Yes, that's the right solution but be aware search engines, especially google may punish the site if not all pages have the mobile redirect.
User avatar
paulbaker
Dev Team Member
Dev Team Member
Posts: 1465
Joined: Sat Apr 18, 2009 10:09 pm
Contact:

Re: How to add javascript to <head> tag?

Post by paulbaker »

Please add [solved] to the subject of the first post.
psy
Power Poster
Power Poster
Posts: 463
Joined: Sat Jan 22, 2005 11:19 am

Re: How to add javascript to <head> tag?

Post by psy »

One more tip before topic is closed.

When you have a mobile domain with a different URL, eg "m.mysite.com", you need to submit a separate sitemap in the mobile XML format. See google webmasters for details. Otherwise your sites will be punished for duplicate content.

Had this happen to couple of my sites. Went from #1 to zero then back again after the mobile specific sitemap was submitted.

Hth,
Psy
Post Reply

Return to “CMSMS Core”