Asked  7 Months ago    Answers:  5   Viewed   23 times

How do you redirect to a page from another page with JavaScript?

 Answers

32

To redirect to another page, you can use:

window.location = "http://www.yoururl.com";
Tuesday, June 1, 2021
 
LukeP
answered 7 Months ago
29

If you're using PHP, do it on the server:

if( isset( $_POST['answer'])) {
    header( 'Location: http://www.example.com/newurl');
    exit();
}

Note that you should be using a full URL in the header() call, and that you likely want to exit() after calling header() in this case.

Saturday, May 29, 2021
 
mdevils
answered 7 Months ago
69

In Ruby on Rails 4, removing www. from any URL whilst maintaining the pathname can be achieved simply by using:

# config/routes.rb

constraints subdomain: 'www' do
  get ':any', to: redirect(subdomain: nil, path: '/%{any}'), any: /.*/
end

In contrast, adding www. to the beginning of any URL that doesn't already have it can be achieved by:

# config/routes.rb

constraints subdomain: false do
  get ':any', to: redirect(subdomain: 'www', path: '/%{any}'), any: /.*/
end
Saturday, July 31, 2021
 
Gopal Biswas
answered 4 Months ago
30

(added in Pycharm 5)

In the Edit Configurations screen under Logs tab check the option: Save console output to file and provide a FULL PATH to the outputfile. thats it - works like magic

enter image description here

Tuesday, August 3, 2021
 
Ronedog
answered 4 Months ago
66

Cross-browser event handling is much simpler with a toolkit like JQuery:

HTML:

<button data-param="foo">Foo</button>
<button data-param="bar">Bar</button>

JQuery code:

$('button').click(function(){
    window.location = window.location.href + '?param=' + $(this).data('param');
});

Demo: http://jsfiddle.net/6DK5J/

Thursday, August 19, 2021
 
Josip Valčić
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share