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

Browser control: Getting and setting the URL another way

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

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



Find the interactive coding exercises for this chapter at:

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



252



78

Browser control:

Forward and reverse

You can make the browser go back one URL in the browser history, as if the user has

pressed the Backspace key or clicked the browser's back button.

history.back();



To make the browser go forward in the history, as if the user has pressed alt-right-arrow

or clicked the browser's forward button...

history.forward();



In both cases, if there is no URL in the history that would make the move possible, the

browser does nothing.

You can tell the browser how many steps in the history you want to take, using negative

numbers to go back and positive numbers to go forward. The following statement is the

equivalent of pressing the Backspace key three times.

history.go(-3);



The following statement sends the browser forward two URLs.

history.go(2);



If a negative number inside the parentheses is greater than the number of previous URLs

in the history, the browser will do nothing. If a positive number inside the parentheses is

greater than the number of forward URLs in the history, the browser will do nothing.

When you want to go forward or backward just one step, you can choose either method..

history.back();



...is the same as...

history.go(-1);



And...

history.forward();



...is the same as...

253



history.go(1);



There is no reliable way to find out how many items, backward and forward, there are in

the history.

If the user clicked a link to get to the current page, you can get the URL of the page where

the link was clicked.

var whereUserCameFrom = document.referrer;



The statement above assigns the URL where the link was clicked to the variable

whereUserCameFrom.

However, this works only if a link was clicked, including a link in a search result. If the

user got to your page through a bookmark or by entering your URL in the address bar, the result

of document.referrer will be an empty string, "".



254



Find the interactive coding exercises for this chapter at:

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



255



79

Browser control:

Filling the window with content

Popup windows are the Rodney Dangerfield of browser features, thanks to their abuse by

aggressive marketers. But they have their legitimate uses. If you plan to use them, you'll have to

deal with popup blockers, which you'll learn to do shortly. This chapter and the next one teach

you the code that creates the window.

This is the basic, barebones statement.

var monkeyWindow = window.open();



The code above opens a blank window of maximum size and gives it a handle, a variable

that refers to this particular window—in this case, monkeyWindow. Depending on the browser,

the window may open on top of the previous window, in a new tab, or even in a new copy of

the browser. You can't control this.

There are three ways to fill a new window with content, You can use the write method to

put HTML content on the screen...

1 var monkeyWindow = window.open();

2 var windowContent = "

Capuchin monkey

The word capuchin derives from a

group of friars
named the Order of Friars Minor Capuchin who wear
brown

robes with large hoods covering their heads.

";

3 monkeyWindow.document.write(windowContent);



Here's the line-by-line breakdown:

1. Opens a new window and assigns it the handle monkeyWindow

2. Assigns text to the variable windowContent

3. Fills the window with the text

Things to notice:

Inside the main quotes that enclose the whole string, any quoted text must be in single

quotes, as in .

Using the document.write method, you place the HTML string on the page. You

designate the window by its handle, the variable that you declared when you opened the

window.

The document.write method in this example is used to fill an empty window with

256



content. You could also use it to write to a window that already has content. But it will

overwrite all the HTML of the original document, replacing its content entirely.

Instead of assigning the HTML string to a variable and specifying the variable inside the

parentheses, you could just put the HTML string inside the parentheses, enclosed in

quotes of course. But this would be even more unwieldy than the example code.

The second way to fill the window with content is to assign a document to it, as you

learned to do in previous chapters.

monkeyWindow.location.assign("http://www.animals.com/capuchin.html");



...or...

monkeyWindow.location.href = "http://www.animals.com/capuchin.html";



The third and most common way to fill the window with content is to include the

document assignment in the statement that opens the window.

var monkeyWindow = window.open("http://www.animals.com/capuchin.html");



If the document you're opening in the popup shares the same host and directory as the

original document, you can just write...

var monkeyWindow = window.open("capuchin.html");



This is how you close a window.

monkeyWindow.close();



257



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

Browser control: Getting and setting the URL another way

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

×