Make browser go to different url on button click? - javascript

I've got a jquery-ui button. When clicked, I want to move the browser to a url within my domain:
<button onclick='foo'>Go</button>
function foo() {
window.location('/otherpage');
}
is that the right way to do it, and address it relatively? Is there a better way to do this?
Thanks

$('#button-id').click(function() { window.location.pathname = '/otherpage'; });
What do you mean by relatively? To the current page or to the current host? There are a few options you are able to use.

It should be window.location = '/otherpage'; Other than that I think it's fine.

Related

Changing URL to something else?

If I have a site "http://example.com/" is it possible to show in the URL (after loading the page) "bananas" instead of "http://example.com/"?
No, this is not possible without redirecting to another page.
This isnt possible with a redirect. You cant do it anywhere
Closest you can get is window.location.hash = "bananas"; which produces http://example.com/#bananas.

Javascript to hide div and href in the same link

I have a link that sets a cookie and hides a DIV (intro) when clicked. I however also want it to go to a URL (a href) when clicked, while still running the code to hide the div and set the cookie. How do I achieve this?
Currently I have the cookies code:
$(document).ready(function() {
if (!readCookie('my-intro')) {
$('#intro').show();
}
$('#close').click(function() {
$('#intro').hide();
createCookie('my-intro', true, 1)
return false;
});
});
(Note there's more code for the "createCookie" function, but I don't think that's relevant to the problem, so I removed it to keep it cleaner).
And the current link:
close and go to URL
The problem is with the above, I need it to close AND go to a URL, which I can't seem to set as I already have a "href" with javascript.
Thank you very much in advance for your help.
Just enter your link under the href attribute, and return true at the end of your click function.
You can more-or-less duplicate what a normal <a> click does by just setting window.location:
window.location = "http://what/ever";
Now, that said, it's not really clear what the point would be of adjusting page layout when you're about to obliterate it all anyway.
If I understand you properly I would do the following:
Give the link these attributes:
Close and go
And then do a
window.location = $( '#close' ).attr ( 'href' );
I think you can use the following code-
<div id="div1">click here to go to www.yahoo.com</div>
You have to define the setCookie() function to set the cookie.

selector back button history web

Would need to know the selector button to return to the previous page, for jquery or javascript.
to be understood would be something like:
('back-button').click(function(){
});
I want this and I need that when you click back to return to the home of my web.
Thank you very much
HTML:
<button id="back-button">Go Back</button>
jQuery:
$('#back-button').click(function(){
window.history.back();//go back one page
});
Other possibilities:
window.location.history.go(-1);//go back one page
window.history.back();//go back one page
window.location.href = 'http://www.google.com'; //go to google.com
It's about as clear as mud, but I think he is either after this:
('#back-button').click(function(){
location.href = "/urlofyourhomepage/";
});
Or this:
('#back-button').click(function(){
window.history.back()
});
With a button being on the page with an ID of back-button.
Go Back
I don't (or at least I hope not) think that he wanted to intercept the back button functionality of the browser.

jqm data-rel="back" issue

imagine the following scenario:
i have a jquery-mobile formular, it´s results are linking to its resultpage.
on the resultpage i have this back button:
this works fine to just update the content and keep the submitted form data,
but
what if a user came from a search-engine or similiar extern link, then my back button links back to the searchengine/externLink .
so how do i Differentiate between those who came from my form or anywhere else in a jqm-way ?
i have a "start-search-page" i would love to link to if the user didn´t came from the search and i don´t want to miss the ajax-link from my search to the resultpage, use the same button and idealy i don´t have to set any cookie.
is there any hint or smarter attempt than check the server url from document.referrer ?
thanks in advance
You can check current page url using below code:
var prevUrl = $.mobile.activePage.data('url');
in case u want to perform different actions based on previous URL.
then on save the URL in the global javascript variable and on click of the button check the previous URL and do the your functionality. eg
Before Navigating to page:
var prevUrl = $.mobile.activePage.data('url');
on click of button:
if (prevUrl=="myurl") {
//do something
$.mobile.changePage('#search')
}
else {
$.mobile.changePage('#nothing')
}

Changing shown URL after GET Request

Currently I have 2 pages based on the following URL MyNewApp/purchase and MyNewApp/confirm. On the 2nd page that is /confirm I added a back button with the following
HTML
<button type="submit" class="btn" id="back">
Go Back
</button>
JS
$('button[id="back"]').click(function() {
this.form.action="backToPurchase";
this.form.method="GET";
});
After pressing the back button everything works fine except the URL changed to /MyNewApp/backToPurchase?_method=. How can I set the URL to /MyNewApp/purchase after pressing the back button ?
You Can't just change the URL as you wish.
Think about it, if you could change the URL like that it would be a big security problem.
BUT try to use POST instead, or just send the user to a new page so the URL will change, You have lots of options to solve it, just think.
This is technically the answer to your question, though you might want to think about a better way to do what you want.
window.history.pushState(null, null, “/new-url”);
I would recommend a better option, instead of submitting a form just for redirection, use the following:
$('#back').click(function() {// <-- selecting by id is much faster than selecting by attribute "id"
location.href = '/backToPurchase'; // or '/purchase';
// OR you can do the following :
history.back(); //this will performs as you clicked the back btn from the browser
});

Categories

Resources