Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6911467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:57:35+00:00 2026-05-27T08:57:35+00:00

I’ve got the following code that get’s a URL parameter, selects an option from

  • 0

I’ve got the following code that get’s a URL parameter, selects an option from a select element and then hides the div the select is in.

It’s going to be on a form that supplied by a 3rd party service, I only have access to a js file in the head of the form, there’s lots of other javascript. jQuery can’t be used (it’s not in the head).

What I’m unsure of is how to run the code after the rest of the page has loaded (or at least the elements needed), I can’t use window.onload, and I want to wrap things up as much as possible to avoid any chance of variables with same names elsewhere.

Thanks.

function getURLparameter( variable ){
variable = variable.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+variable+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
    return "";
else
    return results[1];
}
var typeNum = {
    abc: {
        redirectURL: 'http://www.1.com',
        optionIndex: 1
    }, 
    123: {
        redirectURL: 'http://www.2.com',
        optionIndex: 2
    },
    //etc etc
};

var formId = getURLparameter('SurveyID');


if (formId === 384) {
    var urlType = getURLparameter('type');
    var theType = typeNum[urlType];
    var selectDiv = ('the_div');
    var selectField = ('the_select');
    if (urlType in typeNum) {
        document.getElementById(selectField).options[theType.optionIndex].selected = true;
        document.getElementById(selectDiv).style.visibility = "hidden";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:57:36+00:00Added an answer on May 27, 2026 at 8:57 am

    You need to put it at the bottom of the page below all the elements you are accessing in the script.

    If you want to wrap it then use self-executing anonymous function:

    (function() {
      // all your code should be moved here
    })();
    

    This way no variables and functions defined inside will be accessible outside.

    Update (see comments):

    If window.onload is used by another script you can still add your own code there:

    // previous onload function - defined in another script for example
    window.onload = function() {
      document.getElementById("test").innerHTML = "hello";
    };
    
    // we will add another function to onload
    window.onload = (function() {
      // previous onload function
      var oldonload = window.onload;
    
      // return new onload function ...
      return function() {
        // .. that will call previous onload
        oldonload();
        // and add something extra
        document.getElementById("test").innerHTML += " world!";
      };
    })();
    

    HERE is the code.

    EDIT:

    A comment by symcbean:

    Or rather than using a closure, just use window.addEventListener() http://developer.mozilla.org/en/Code_snippets/On_page_load

    For IE versions < 9 attachEvent() must be used. See this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.