开发者

Call "onclick" function with url load

开发者 https://www.devze.com 2023-03-16 13:12 出处:网络
I have a URL f.e. http://localhost/index.php And on that index.php I have a simple JS function. I can call this function if I type \"javascript:function\" in the URL bar.

I have a URL f.e.

http://localhost/index.php

And on that index.php I have a simple JS function.

I can call this function if I type "javascript:function" in the URL bar.

So far so good, but I want to combine this function with the Url.

F.e.

http://localhost/index.php?javascript:function

Is this even possible? To开发者_如何学Go post links and call JS functions from distance?

I will wait for your answers. Thank you.


You can do

function clickFunction()
{
  do stuff
}

in your link call

onclick="clickFunciton()"

and in php call

if(isset($_REQUEST['javascript:function'])){
    echo "<script type='text/javascript'>clickFunction()</script>";
}

then it will be called on page load

or call

window.onload=clickFunction() ;

and Something like this can get you the query params

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


I'm not sure whether this is actually achievable but as stated above, i'd seriously reconsider the way it is being implemented, as anybody could call functions by simply typing them into their browser URL.

Imagine - localhost/?delete_user_from_db()

Happy to help if you need more advice on how to change what you are trying to do.


Well if you are using jQuery you should be able to do some checking of the url in document ready...

$(document).ready(function() {
    // put all your jQuery goodness in here.
});

I don't know off the top of my head if / what the js function is that gets called when the page is done loading.

Then you should be able to something along the lines of... http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

To call the method. I don't know if you could call the js directly from the URL but this might be a work around, instead of using the js function you'd need to set a flag and then you could trigger the method.

Hope this helps.


If you mean that you want to call a function when you click some link, then you can easily do this:

<a href="javascript:your_function_name();">Click Me</a>

or

<a href="#" onclick="your_function_name(); return false;">Click Me</a>

0

精彩评论

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

关注公众号