Simulate link click from function that directs to localhost - javascript

So I am trying to simulate a link click when a button is clicked, but nothing seems to happen when executing the code.
<a id=“preface” href=https://localhost:443/6.5.1> 6.5.1 </a>
<button id=“auto” onclick="myFunction()">Click</button>
<script>
function myFunction() {
document.getElementById(“preface”).click();
}
</script>
I should mention that the template file containing this code is used and executed by Golang with:
s1, _ := template.ParseFiles("html_file")
s1.ExecuteTemplate(w, "html_file", nil)
I do not know why it does not work, because when I try doing an alert action it works, while even if I try opening a window for www.google.com it does not work.

That's not how you redirect using js. try to use window.location.
Something like:
function myFunction() {
var link = document.getElementById("preface").getAttribute("href");
window.location.href = link;
}
Edit:
Plunker here:
https://plnkr.co/edit/AP7MV51LdRs43HFIQngw?p=preview
If you see in the console..network tab you should see a call to google.co.uk

Thank you for your help! Apparently the problem was that the id value was written with quotes "preface". When I wrote it:
id = preface
it worked. I do not understand why, since as far as I know both href and id should have their values in quotes, but it seems that it only works this way.
Thank you again!

Related

window.location.href not working when "isset['buttonname']" is occurring (php js)

As the title says (window.location.href not working when “isset['buttonname']” is occurring (php js)), my page is not redirecting to my new page that I want it to go to. I have tried using :
     window.location.href = 'reviewer.php';
    </script>
and have also tried using
echo "<script> location.href='http://gwupyterhub.seas.gwu.edu/~rkanungo/clout_computing/reviewer.php'; </script>";
Neither one of these scripts are being executed, and I insert them earlier in my code to see if they would even be executed and still nothing, even right after the initial if clause. Any suggestions?
I suggest that you try the Javascript document ready function with window.location.replace(), for example.
<script type="text/javascript">
(function() {
window.location.replace("https://example.com");
})();
</script>
Alternatively you can only use the window.location.replace() function if the above doesn't work.
The PHP header() function must be called before any actual output, for example "echo".
Sorry folks, I figured it out. Was simply not on the correct url while changing and none of my changes were showing up. Thank you all though!

Adding onclick=”GoTo(‘URL’) to a WordPress post

I want to add JavaScript attribute “onclick” with “go to” function inside a “span” tag: onclick=”GoTo(‘URL’) to a post on WordPress website.
When I add this code to a post:
<span class="aff" onclick="GoTo('URL')" target="blank">text</span>
nothing happens when you click on the "text" and there is an error “Uncaught ReferenceError: GoTo is not defined at HTMLSpanElement.onclick” at the console.
Please, help to fix the issue.
Add this ->
<script type="text/javascript">
var GoTo=function(url){
window.location.replace(url);
}
</script>
Can you add the code for your Goto function hard to know what wrong without that?
Assume something like
function GoTo(url){
window.location = url;
}

Html button with location.href and document.referrer together

I am trying to make this work
<script>
document.write('<button onclick="location.href=' + document.referrer + '">Go back</button>');
</script>
It should be nothing but a simple "Back" button to the previsous page.
When I do it like an "a href" it works
<script>
document.write('Go back');
</script>
but when trying tom ake it as a "button" it fails. The button is there but didnt navigate to the previsious page
Add another escaped quote into the href body (as well as a semicolon at the end, although I don't know if that would make a difference) and see if that works...
<script>
document.write('<button onclick="location.href=\'' + document.referrer + '\';">Go back</button>');
</script>
You could also implement native back with:
window.history.back();
//in your onclick="history.back()"
//also can try with:
//javascript:history.go(-1)">
Or call a function that check if history.back() is available in the browser first else implement a custom back function/code as is showed here (I would prefer take out the code from the html just handle the onclick in my function)
Using javascript history.back() fails in Safari .. how do I make it cross-browser?
For a robust solution check history.js:
https://github.com/browserstate/history.js
You're missing the quotes around the URL in the href, so try escaping it like this (worked for me)
document.write('<button onclick="location.href=\'' + document.referrer + '\'">Go back</button>');

Onclick in <a> cause reloading of the page insted of executing JS code

My URL: http://www.example.com/?product=3&url=XYZ
My code:
link
After click, page is reloading, instead of executing foo() function.
JS is enabled. Body of foo():
function foo() { alert("sss"); }
Probably, this problem is caused by URL of my site. XYZ parameter is a url of a website but with something like "%C5%82%C3%B3" instead of special characters (something like after using htmlspecialchars()).
What is interesting, after click the page is reloaded with the "normal" URL, something like: http://www.example.com/?product=3&url=http://www.example.com (WITH special characters like " / ").
What can I do to resolve this problem?
EDIT
Above code works fine. Thank you for your time.
Yeah, there's definitely something else going on here. Here's a minimal example that works just fine:
<html>
<body>
link
<script type="text/javascript">
function foo() { alert("hi"); }
</script>
</body>
</html>
Assigning onclick inline is not a good practice, and you should be doing something like
<a id="someId" /* ... */ >
// ...
<script type="text/javascript">
function foo() { alert("hi"); return false; }
document.getElementById("someId").onclick = foo;
</script>
but in any case, the most likely culprit is that your script has a syntax error somewhere and is not loading at all. You can verify this by setting onclick="return false". If that doesn't work, it's likely you have some other event handler that's being triggered. But because the above -- all we know of your code -- works, it's unlikely anyone here can diagnose what the problem is without more information.
Edit
Nevermind this, I fully assumed your code wasn't working and the below did. But I guess the code you posted was already correct.
On a seperate note, you should avoid using attributes like onClick, create event handlers instead (I suggest looking at jQuery).
i tried the same code in my systm.the code is workin and the alert box is visible.
My code:
<a href="#" onclick="foo();return false;">link/a>
and javascript is
<script type="text/javascript">
function foo()
{
alert("sss");
}
</script>
i am using vs2008

windows.location.href not working on Firefox3

We have a JavaScript function named "move" which does just "windows.location.href = any given anchor".
This function works on IE, Opera and Safari, but somehow is ignored in Firefox. Researching on Google doesn't produce a satisfactory answer why it doesn't work.
Does any JavaScript guru knows about this behavior, and what would be the best practice to jump to an anchor via JavaScript?
Have you tried just using
window.location = 'url';
In some browsers, window.location.href is a read-only property and is not the best way to set the location (even though technically it should allow you to). If you use the location property on its own, that should redirect for you in all browsers.
Mozilla's documentation has a pretty detailed explanation of how to use the window.location object.
https://developer.mozilla.org/en/DOM/window.location
If you are trying to call this javascript code after an event that is followed by a callback then you must add another line to your function:
function JSNavSomewhere()
{
window.location.href = myUrl;
return false;
}
in your markup for the page, the control that calls this function on click must return this function's value
<asp:button ........ onclick="return JSNavSomewhere();" />
The false return value will cancel the callback and the redirection will now work. Why this works in IE? Well I guess they were thinking differently on the issue when they prioritized the redirection over the callback.
Hope this helps!
One observation to ensure in such a scenario
Following will work in IE, but neither in Chrome nor in Firefox (the versions I tested)
window.location.href("http://stackoverflow.com");
Following will work all the three
window.location.href = "http://stackoverflow.com";
Maybe it's just a typo in your post and not in your code, but it's window and not windows
I am not sure to follow you.
I just tried: going with FF3 to Lua 5.1 Reference Manual (long and with lot of anchors).
Pasting javascript:window.location.href="#2.5"; alert(window.location.href); in the address bar, I went to the right anchor and it displayed the right URL. Works also with a full URL, of course.
Alternative code: javascript:(function () { window.location.href="#2.5"; })();
Perhaps you forgot the #. Common problem, also with image maps.
I have the same problem and I guess this is related to a click event.
I have a function that moves the browser to a specific page. I attach that function to some click events: in a button and in a image. AlsoI execute the function when the user press escape (document onkeypress event).
The results are that in all cases the function is called and executed, but only when there is a click the browser goes to the address I want.
Update
I got it working! with a
setTimeout( "location.replace('whatever.html');", 0 );
I don't know why the location.replace wasn't working when the event was a keypress, but with the settimeout it works :)
Update
Returning false after the event when you press escape makes the redirection works. If you return true or nothing the browser will not follow
You've got to add return false; after the window.location.href as mentioned above.
function thisWorks()
{
window.location.href = "http://www.google.com";
return false;
}
function thisDoesNotWork()
{
window.location.href = "http://www.google.com";
}
window.location.href works fine in all versions of Firefox, as does document.location.href I think that there is something else in your code that is breaking things.
drop this in a blank page, if it works, it indicates there is something else wrong on your page.
<script>
window.location.href = 'http://www.google.com/';
</script>
You could also use window.location.replace to jump to an anchor without register it in the browser history:
This article illustrates how to jump to an anchor and uses href as read-only property.
function navigateNext()
{
if (!window.location.hash)
{
window.location.replace(window.location.href + unescape("#2"))
}
else
{
newItem = nextItem(window.location.hash)
if (document.getElementById(newItem))
{
window.location.replace(stripHash(window.location) + "#" + newItem)
}
else
{
window.location.replace(stripHash(window.location) + "#1")
}
}
}
Have you tried this?
Response.Write("<script type='text/javaScript'> window.location = '#myAnchor'; </script>";);
please add full javascript script tag
<script type="text/javascript" language="javascript"></script>
window.location.hash = "#gallery";
For reference I had the same problem.
onclick = "javascript: window.location('example.html');" didn't work under FF (latest)
I just had to rewrite to onclick = "javascript: window.location = 'example.html';" to get it working
I just overcome the same problem. and the problem is not in javascript, but the href attribute on the <a> element.
my js code
function sebelum_hapus()
{
var setuju = confirm ("Anda akan menghapus data...")
if (setuju)
window.location = "index.php";
}
my previous html was
Klik here
and I update it to
Klik here
or remove the href attribute
hope this helps.
window.location.assign("link to next page") should work in both (chrome and firefox) browsers.
window.location.assign("link to next page")
Another option:
document.location.href ="..."

Categories