adding custom javascript to "head" section of post / page

Community Forums Forums Design Tips and Tricks adding custom javascript to "head" section of post / page

This topic is: not resolved

This topic contains 3 replies, has 3 voices, and was last updated by  jhguynn 8 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #96287

    jhguynn
    Participant
    Post count: 223

    I want to redirect mobile users on a page/post basis to a corresponding page/post located at a “m.URL.com” mobile themed site. In other words, I don’t just want to install a mobile redirect plugin that sends mobile users to the home page of “m.URL.com”

    If they land on http://www.URL.com/page-A, I want to redirect them to http://m.URL.com/page-A

    If they land on http://www.URL.com/page-B, I want to redirect them to http://m.URL.com/page-B

    etc.

    I can do this with a page/post specific javascript call that needs to be placed in the <head> section of each page or post. How can I accomplish this? Does the “scripts” field accomplish this by placing entries there into the <head> section? If not, where are those scripts placed?

    Thanks for the help.

    • This topic was modified 8 months ago by  jhguynn.
    • This topic was modified 8 months ago by  jhguynn.
    • This topic was modified 8 months ago by  jhguynn.
    • This topic was modified 8 months ago by  jhguynn.
    #96342

    AC
    Blocked
    Post count: 7712

    If you are talking about the scripts box found on the pages/posts edit screens, then I would think the answer is yes.

    #96297

    braddalton
    Participant
    Post count: 10568

    The scripts box on each Edit screen will do that or you can hook it in conditionally using wp_enqueue_script

    You could also use wp_redirect() rathr than Javascript.


    #96436

    jhguynn
    Participant
    Post count: 223

    Anitac and Brad – thanks. When I add the javascript (shown below) to the edit screen and save, it doesn’t seem to take. The page refreshes after save and the field is blank, as when I began. Here is the code I’m trying to add:

    <script type="text/javascript" language="JavaScript">
    	var mobileSiteURL = "http://m.lifepoplicydetective.co.uk/landing-page-1";
    </script>
    <script type="text/javascript" src="/wp-includes/js/mdetect.js"></script>

    Obviously, I uploaded ‘mdetect.js’ to the location indicated above.

    Testing the redirect on my mobile phone does not produce the desired result. Is what I’m doing supposed to work as I have implemented it?

    PS: Brad – can I use wp_redirect() on a page by page and post by post basis? Do I enter that code into the same script box on each page or post? I’m not sure how to accomplish it this way.

    • This reply was modified 8 months ago by  jhguynn.
    • This reply was modified 8 months ago by  jhguynn.
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.