开发者

Posting data into JavaScript from an URL

开发者 https://www.devze.com 2023-01-16 19:16 出处:网络
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 ?

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 te开发者_StackOverflow社区st-test.js, by putting in the URL in a browser with some values for e,e..


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.)


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.


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 */
})();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号