Call JavaScript Function from Url/Address Bar

Call Javascript function from URL/address bar

There isn't from a hyperlink, no. Not unless the page has script inside specifically for this and it's checking for some parameter....but for your question, no, there's no built-in support in browsers for this.

There are however bookmarklets you can bookmark to quickly run JavaScript functions from your address bar; not sure if that meets your needs, but it's as close as it gets.

Call javascript function straight from url address bar?

It's difficult to understand what you're asking for here, perhaps when on the page you want to run a function such as?

function getMP4Strings(s) {
var i = -1, j, k, a = [];
while (-1 !== (i = s.indexOf('mp4', i + 1))) { // for each "mp4"
j = s.lastIndexOf('"', i) + 1; // get previous "
k = s.indexOf('"', i); // get next "
a.push(s.slice(j, k)); // store string between them
}
return a;
}
getMP4Strings(document.body.innerHTML);
/* [
"mp4:rbb/heimatjournal/sendung/heimatjournal_20130706_sdg_m_16_9_512x288.mp4",
"mp4:rbb/heimatjournal/sendung/heimatjournal_20130706_sdg_m_16_9_512x288.mp4",
"http://http-stream.rbb-online.de/rbb/heimatjournal/sendung/heimatjournal_20130706_sdg_m_16_9_512x288.mp4"
] */

Execute Javascript method from browser address bar - GWT

I believe most browsers refuse to execute javascript: URLs from the URL bar as a safety measure (there's been messages on the web –esp. Facebook– telling people to copy-paste a javascript: URL to their URL bar that triggered an XSS). They didn't want to break bookmarklets though, so you can put that code in a bookmark; but it's much easier to just open the browser's JS console and type that command there.

Anyway, it's not due to GWT's DevMode.

call JS function via url

<a href="javascript:foo(bar)">foo</a> represents the calling of a function, not the call to a url. There is no direct mapping between a url and a JavaScript function.

Executing JavaScript from the address bar causes the browser to exit the page?

Add a void(0); to the end and it will fix it.

Best (and secure) way to call javascript function (with arguments) using a url from another page

For example you can append some parameter at the end of your URL
https://your-url/?parameter=hello

When this URL is opened on another webpage you can run JavaScript or a PHP function based on that URL query.

For JavaScript

getUrlParam(slug) {
let url = new URL(window.location);
let params = new URLSearchParams(url.search);
let param = params.get(slug);
if (param) {
return param;
} else {
return false;
}
}

console.log(getUrlParam('parameter'));

Calling external javascript function through url bar

If you need the code to execute once it is loaded you can do one of two things:

  1. Execute functionToCall(document.body.innerHTML); at the bottom of your script (http://www.url.of/external/js/file.js) rather than at the end of your bookmarklet.

  2. Use e.onload = function(){ functionToCall(document.body.innerHTML); }; after e.type='text/javascript' near the end of your JavaScript snippet / bookmarklet, rather than calling functionToCall right after appending e to the document head (since e will most likely not have been loaded and parsed right after appendChild(e) is called.

JS function not changing URL in address bar

You are trying to call the jQuery is() method on a DOM object, which doesn't have one. This would be obvious if you looked at your browser's JavaScript console.

Check the checked property instead.

if(document.getElementById('armsCheck').checked){

Also replace these lines:

window.location.search = "";
window.location.href = window.location.href + "?bodypart=Arms";

with:

window.location.search = "?bodypart=Arms";

As the first line would trigger a page reload before reaching the second line.


There's no need to involve JavaScript at all though. You can get the same effect with plain HTML.

<form>
<label><input type="checkbox" name="bodypart" value="Arms"> Arms</label>
<input type="submit" value="Submit">
</form>

Disallow using Javascript on URL address bar

I agree with the other commenters that detecting "where the call to the function was made and if it was not from the address bar then the function should run" is a bad way to approach client-side security, insofar as there is such a thing.

That said, function scope, closures, and how this relates to the URL bar is an interesting topic. Here's some more context on global variables and scope. The short version is that if you have a function like this:

function test (argument) {
alert('hey')
}

It will be executable via the URL bar because it's in the window/global scope, which seems to be as far as javascript URI's will go. Whereas if you put that same function in a closure:

(function() {
function test (argument) {
alert('hey')
}
})()

...it should be inaccessible as far as executing the function in the URL bar goes.

I would be curious to learn the history of why browser vendors implemented Javascript-via-the-URL...it now has practical usage with bookmarklets and the like, but it doesn't seem to be well-documented.



Related Topics



Leave a reply



Submit