• I would like to be able to insert this JavaScript code into a single page
    and cannot seem to find a method that works. Any ideas?

    <SCRIPT language="JavaScript" For="window" Event="onload">
    <!--
    if(indexSelect[0].checked){indexSelect[0].click();}else if(indexSelect[1].checked){indexSelect[1].click();}else if(indexSelect[2].checked){indexSelect[2].click();}
    else if(indexSelect[3].checked){indexSelect[3].click();}
    if (indexSelect[1].checked) {
    if(document.hotForm && document.hotForm.cityText) {
    document.hotForm.cityText.focus();
    }
    }
    //-->
    </SCRIPT>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

Viewing 11 replies - 1 through 11 (of 11 total)
  • Ayman

    (@aymanalzarrad)

    save your script to a file and then use wp_enqueue_script() to add your custom scripts.
    learn more about wp_enqueue_script()

    To put it into a single page than you can put the wp_enqueue_script() into an if() statment.

    Do not edit the Twenty Twelve theme. It is the current default WordPress theme and having access to an original, unedited, copy of the theme is vital in many situations. First create a child theme for your changes. Or install a custom CSS plugin.

    Thread Starter twebdonny

    (@twebdonny)

    Thanks but that wp_enque_script usage is a bit advanced for me
    to use.

    Is there not some type of usable plugin that would work within
    twentytwelve that allows one to just add the javascript code
    to the page itself?

    In html is was as easy as just pasting that code snippet within the
    page and it would work.

    ps: I do have a child page setup but did not think this was
    a place where that would be necessary as I am just wanting to include
    the javascript in a single individual page

    Is there not some type of usable plugin that would work within
    twentytwelve that allows one to just add the javascript code
    to the page itself?

    Not that I’ve come across myself.

    Ayman

    (@aymanalzarrad)

    you have to add it thou code. As the page template is a general template and applies to all generated pages.

    so to add the script to a specific page you can:

    1- Create a file and put your script in it.
    3- Create a folder under your child theme root folder and call it “js”. so the bath is something ../themes/my-child-theme/js/
    4- Upload your script file to the “js” folder you created before.
    2- Add this code to the functions.php file of your child theme:

    /* First we tell WordPress to check if its the page we need. and then to
     * load the script
     * change the "About" to the page title you want the script to be
     * loaded on.
     * replace 'my_script' with the file name of your script
     */
    function my_custom_script() {
                  if ( is_page( 'About' ) ) {
                           wp_enqueue_script( 'my_script', get_template_directory_uri() . '/js/my_script.js');
                  }
    }
    add_action( 'wp_enqueue_scripts', 'my_custom_script' );

    Like that we did tell WordPress to check if the page we need is the page that is being loaded and if yes, then to add our script to the header.

    Thread Starter twebdonny

    (@twebdonny)

    Thanks,

    I think I’ll have to find another option for implementing
    my search applet because that just appears much too complicated
    for my limited abilities at this point.

    but regardless, thanks again and I am sure this will benefit
    other users who may come across it in the future

    Ayman

    (@aymanalzarrad)

    The easiest way would be to add the code into the page template “page.php”, but than wouldn’t be the right way to do it. If you dicide to do so than just copy and paste the below code into page.php

    [moderated – do NOT modify the default theme]

    @ayman – please do not suggest that people modify the default theme EVER. That’s poor advice.

    Ayman

    (@aymanalzarrad)

    please do not suggest that people modify the default theme EVER.

    @wpyogi, you are 100% right, It’s my mistake that I didn’t explain well that all modifications should be done to a child theme.

    poor advice

    Wasn’t done on purpose.

    Glad to know we’re on the same page about that – thanks ??

    Thread Starter twebdonny

    (@twebdonny)

    I am going to attempt to use “PureHtml” plugin and see if I can work around this. I just want that JavaScript Search code only on specific pages within the site.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘JavaScript in WebPage’ is closed to new replies.