How to Change a Js Variable From an Address Bar

Written by kevin lee
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Change a Js Variable From an Address Bar
Change variables on any Web page by typing in the browser's adress bar. (laptop closeup image by Tasha from Fotolia.com)

JavaScript is a Web programming language that adds interactivity to Web pages. JavaScript runs inside an HTML page or in a separate .js file that contains the JavaScript code. Usually, JavaScript functions within a Web page change the values of the variables in the code. However, anyone can modify the value of any variable on a page by typing a JavaScript command in the address bar of the Web browser. This allows users to alter the look of a Web page and even change the way it functions.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create a new HTML document and add the following code to the <script> section of the document:

    var count = 1;
    function show Count() {
        alert("count = " + count);
    }
    

    This function will display the value stored in the variable named count. When the Web page loads, it will initialise the variable and set the value to 1.

  2. 2

    Add this code to the <body> section:

    <input id="Button1" type="button" value="Show Count" onclick="show Count()" />

    When you click this button, the browser will execute the JavaScript function.

  3. 3

    Save the HTML file and open it in your browser. Click the button. The JavaScript alert will show that the value of count is 1.

  4. 4

    Paste this line of code in the address bar and press "Enter."

    JavaScript:count=2; void(0);

    This executes the js code and changes the value of count. The void(0) command prevents the browser from jumping to a new page after you press "Enter."

  5. 5

    Click the button again to verify that count has changed from 1 to 2.

Tips and warnings

  • You don't change anything on a site's Web server by typing code in the address bar. All changes occur in your browser and disappear when you refresh the page. If you need to view a Web page's js code, click the browser's "View" button and select "View Source." This will show you the variables in the JavaScript code. When typing into the address bar, you don't have to capitalise "JavaScript."

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.