1

I have a javascript on my server, and i need to set a value / calling a function inside the javascript when calling a URL. Is there anyway of doing that ?


UPDATE:

<script type="application/x-javascript" src="test-test.js"></script>

Thats how it its loaded on the HTML site. And I want to call the function test(e,e) inside test-test.js, by putting in the URL in a browser with some values for e,e..

3
  • 4
    please expand a little bit. What you mean by "calling a URL"? Are you using AJAX? Commented Sep 21, 2010 at 6:57
  • are you manually putting in the URL into the browser (or clicking a link)? if you are, you'll need to put it in the querystring (as it's a HTTP GET). if you're posting to the URL (HTTP POST), you can read the form post values on the target page and register them out to your script. Commented Sep 21, 2010 at 6:58
  • 2
    Are you running the javascript on the server, or in the client? Commented Sep 21, 2010 at 6:58

3 Answers 3

2

Unless you are using one of the few web servers that employs server-side JavaScript, your script is going to run in the browser after the page is loaded. If you want to include information from the URL in your script (and this assumes that you can use a query string without changing the server's behavior), you can use window.location.search to get everything from the question mark onwards.

This function will return either the entire query string (without the question mark) or a semicolon-delimited list of values matching the name value you feed it:

function getUrlQueryString(param) {
   var outObj = {};
   var qs = window.location.search;
   if (qs != "") {
      qs = decodeURIComponent(qs.replace(/\?/, ""));
      var paramsArray = qs.split("&");
      var length = paramsArray.length;
      for (var i=0; i<length; ++i) {
         var nameValArray = paramsArray[i].split("=");
         nameValArray[0] = nameValArray[0].toLowerCase();
         if (outObj[nameValArray[0]]) {
            outObj[nameValArray[0]] = outObj[nameValArray[0]] + ";" + nameValArray[1];
            }
         else {
            if (nameValArray.length > 1) {
               outObj[nameValArray[0]] = nameValArray[1];
               }
            else {
               outObj[nameValArray[0]] = true;
               }
            }
         }
      }
   var retVal = param ? outObj[param.toLowerCase()] : qs;
   return retVal ? retVal : ""
   }

So if the URL was, say:

http://www.yoursite.com/somepage.html?name=John%20Doe&occupation=layabout

if you call getUrlQueryString() you would get back name=John Doe&occupation=layabout. If you call getUrlQueryString("name"), you would get back John Doe.

(And yes, I like banner-style indents. So sue me.)

Sign up to request clarification or add additional context in comments.

Comments

1

You can use address plugin to be able to pass some condition in urls trough # symbol: http://my_site/my_page#your_condition

in the html you can write something like this:

<script>
$(function(){
// Init and change handlers
    $.address.init().change(function(event) {
            if (event.value == "your_condition")
                 run_my_finction();
    });
    )};
<script>

See this exaple for the futher help.

Comments

0

If you want to execute JavaScript from the browsers' address bar, you can use a self-invoking function:

javascript:(function () {
  alert('Hello World');
  /* Call the JavaScript functions you require */
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.