Tải bản đầy đủ - 0 (trang)
Browser control: Getting and setting the URL

Browser control: Getting and setting the URL

Tải bản đầy đủ - 0trang

window.location.href = "http://www.me.com";



And if you wanted it to land on an anchor...

window.location.href = "http://www.me.com/1.html#section9";



It would be nice if you could use window.pathname = ... to move to a different page

on the current site or window.hash = ... to move to an anchor on the current page, but you

can't. What you can do, though, is query the browser for the domain name and combine that

with the page where you want to go.

1 var currentSite = window.location.hostname;

2 var destination = "http://" + currentSite + "/wow.html";

3 window.location.href = destination;



Here's the line-by-line breakdown:

1. Gets the domain name and assigns it to the variable currentSite. Example:

www.me.com

2. Concatenates the string "http://" with the domain name plus the destination page and

assigns the combo to the variable destination

3. Directs the browser to the destination

This is how to direct the browser to an anchor on the current page.

1

2

3

4



var currentSite = window.location.hostname;

var currentPath = window.location.pathname;

var destination = "http://" + currentSite + currentPath + "#humidifier";

window.location.href = destination;



Here's the breakdown:

1. Gets the domain name and assigns it to the variable currentSite. Example:

www.me.com

2. Gets the pathname and assigns it to the variable currentPath. Example: /1.html

3. Concatenates the string "http://" with the domain name, the destination page, and the

desired anchor and assigns the combo to the variable destination

4. Directs the browser to the destination



247



You can omit window. It's legal to use location.href, location.hostname,

location.pathname, and location.hash. It's more common to include window.

You can omit href when you're detecting the URL. It's legal to use window.location, or

simply location. (See above.) Including href is preferred for esoteric reasons.

You can use document.URL as an alternative to window.location.href

There are two more ways to direct the browser to a url, alternatives to

window.location.href. You'll learn how to use these in the next chapter.



248



Find the interactive coding exercises for this chapter at:

http://www.ASmarterWayToLearn.com/js/76.html



249



77

Browser control:

Getting and setting the URL another way

In the last chapter you learned to direct the browser to a new URL by assigning a string to

window.location.href. Here's another way to do the same thing.

window.location.assign("http://www.me.com");



The statement directs the browser to the home page of me.com.

As with the window.location.href statement, you can make the URL as detailed as you

like.

window.location.assign("http://www.me.com/lojack.html#guarantee");



The statement directs the browser to the anchor #guarantee on the lojack.html page of the

site me.com.

Here's another alternative that has a slightly different effect.

window.location.replace("http://www.me.com/lojack.html#guarantee");



Once again, the statement directs the browser to a new URL. But by using replace

instead of assign, you interfere with the browser history. When you use assign, the history is

intact. The statement takes the user away from the original page, to the new page. If, after

arriving at the new page, she presses the Backspace key or clicks the browser's back button,

she goes back to the original page that she just came from. But when you use replace, the

original page doesn't make it into the history. If the user presses Backspace after being taken

to the new page, she's taken to the page that displayed before the original page since the

original page is no longer in the history. If there is no page before the original page, nothing

happens when she presses Backspace.

To reload the current page code one of these statements:

window.location.reload(true);

window.location.reload(false);

window.location.reload();



All three statements reload the current page. If the argument is true (example 1 above),

the statement forces the browser to load the page from the server. If the argument is false

(example 2) or if there is no argument (example 3), the browser will load the page from the

cache if the page has been cached.



250



You can use window.location.href = window.location.href or any of the

abbreviated alternatives to reload the current page. The reload is faster, but it doesn't

allow you to specify whether the browser reloads from the server or the cache.

document.URL = document.URL doesn't work.



251



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Browser control: Getting and setting the URL

Tải bản đầy đủ ngay(0 tr)

×