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!
This may be a really simple question but despite all my fiddling I can't get it to work and I think others might find it helpful in the future.
I'm using the embed payment button here: https://commerce.coinbase.com/docs/#payment-buttons
I have my script:
<div>
<a class="buy-with-crypto" data-custom="MY_CUSTOM_DATA"
href="https://commerce.coinbase.com/checkout/e690ad8a-8bed-4d6e-a8a7-
b47c2efc456f">
Register
</a>
<script src="https://commerce.coinbase.com/v1/checkout.js?version=201807">
BuyWithCrypto.registerCallback("onPaymentDetected", function(e){
alert("payment detected");
});
</script>
</div>
I've tried putting the function above in separate script tags, tried amending the url with .js?onload=[untold number variables/callbacks] as it details in the docs for initialization but it's still not working.
What I want (and what I believe this is meant to do), is once a payment is detected by the script it activates the function and sends an alert (alert to eventually be replaced by what I actually want but you get the idea).
Got it to work. I have the function in separate script tags above and below (just covering all options). I then amended the src/url with js?onload=BuyWithCrypto(onPaymentDetected) and the alert came through.
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!
I'm trying to get jQuery to take the hashtag from a URL and do some functions with it:
Hashtag example:
http://www.example.com/Help.asp#contact
I built out a help center. This help center hides all the answers on the page load and when you click on a question, the answer slides down. Each question is it's own anchor with a name identifier. So when you go to http://www.example.com/Help.asp#contact, the document automatically scrolls down to <a name="contact">. We use these a lot in emails or whatever when we want to direct a user straight to the question they're asking in the Help Center. However, it would be even easier to use, if that anchor is triggered a second after the page loads. What I'm guessing is, I'd need to grab the hashtag, and have a jQuery script like this:
$(document).ready(function(){
$('a[name="~~HASHTAG~~"]').trigger('click').delay(1000);
});
How can I get the hashtag from the URL? Thanks for your help.
Here's my HTML http://jsfiddle.net/tBLuL/
You are looking for the javascript value
location.hash
It will contain the #Hash part of the url, in your case
#contact
Use the hash property off of the window object ...
$(document).ready(function(){
var thehash = window.location.hash; // #contact
$('a[name="' + thehash.replace('#','') + '"]')
.trigger('click')
.delay(1000);
});
Try this.
$('a[name="' + location.hash.replace('#', '') + '"]').click().delay(1000);
The delay of 1 sec after the click is useless. What are you trying to achieve with delay?
Update:
If you want to delay the click try this.
$('a[name="' + location.hash.replace('#', '') + '"]').delay(1000).click();
Check out the window.location object. location.hash has exactly what you're looking for, no jQuery required.
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 ="..."