Calling a javascript on an external page as a URL
There is a javascript on an external webpage that downloads a file. Can I pass a URL to开发者_如何学Python a local script to download this automatically? Simply using http://webpage.com/javascript:theScript()
doesn't seem to work, even when I type it into a browser. I'm very much novice in this space, be gentle! Thanks in advance.
This is too little info to tell you exactly how to do it (or even if it can be done at all). But what I can tell you:
http://webpage.com/javascript:theScript()
cannot work the way you expect. The URL needs to locate a file, so if webpage.com was hosting a file named exactly `javascript:theScript()', then your browser would download the contents of this file and display it.
If the function you want to call resides in a javascript file, lets say, functions.js (URL: http://webpage.com/functions.js), then you can include this file in your own webpage and call functions from there.
This is where the special javascript:
notation comes to place: It's a workaround to allow you to specify a javascript function in a regular <a href...> tag.
File: functions.js
function theFunction() {
alert ("Hey there!");
}
File: yourpage.html
<html>
<head>
<script type="text/javascript" src="http://webpage.com/functions.js">
</script>
</head>
<body onload="theFunction();">
Your text<br>
<a href="javascript:theFunction();">Click here</a>
</body>
</html>
If this doesn't help you out, then you should post some code (ie. contents of the file containing the function to call, how you want to include the function in your page etc).
To include a script:
<script type="text/javascript" src="http://www.example.com/script.js"></script>
To execute code from the script you'll need a separate script tag:
<script type="text/javascript">
theScript(); //where theScript is a function defined in script.js
</script>
You should look at some tutorials for JavaScript. I recommend w3schools.
精彩评论