/* * Schedule an invocation or invocations of f() in the future. * Wait start milliseconds, then call f() every interval milliseconds, * stopping after a total of start+end milliseconds. * If interval is specified but end is omitted, then never stop invoking f. * If interval and end are omitted, then just invoke f once after start ms. * If only f is specified, behave as if start was 0. * Note that the call to invoke() does not block: it returns right away. */ functioninvoke(f, start, interval, end) { if (!start) start = 0; // Default to 0 ms if (arguments.length <= 2) // Single-invocation case setTimeout(f, start); // Single invocation after start ms. else { // Multiple invocation case setTimeout(repeat, start); // Repetitions begin in start ms functionrepeat() { // Invoked by the timeout above var h = setInterval(f, interval); // Invoke f every interval ms. // And stop invoking after end ms, if end is defined if (end) setTimeout(function() { clearInterval(h); }, end); } } }
/* * This function parses ampersand-separated name=value argument pairs from * the query string of the URL. It stores the name=value pairs in * properties of an object and returns that object. Use it like this: * * var args = urlArgs(); // Parse args from URL * var q = args.q || ""; // Use argument, if defined, or a default value * var n = args.n ? parseInt(args.n) : 10; */ functionurlArgs() { var args = {}; // Start with an empty object var query = location.search.substring(1); // Get query string, minus '?' var pairs = query.split("&"); // Split at ampersands for (var i = 0; i < pairs.length; i++) { // For each fragment var pos = pairs[i].indexOf('='); // Look for "name=value" if (pos == -1) continue; // If not found, skip it var name = pairs[i].substring(0, pos); // Extract the name var value = pairs[i].substring(pos + 1); // Extract the value value = decodeURIComponent(value); // Decode the value args[name] = value; // Store as a property } return args; // Return the parsed arguments }
location = "http://www.oreilly.com"; // Go buy some books! location = "page2.html"; // Load the next page /** * #top不存在时,浏览器跳到文档开始处 */ location = "#top"; // Jump to the top of the document location.search = "?page=" + (pagenum+1); // load the next page
<!-- This is not a stand-alone HTML file. It must be invoked by showModalDialog(). It expects window.dialogArguments to be an array of strings. The first element of the array is displayed at the top of the dialog. Each remaining element is a label for a single-line text input field. Returns an array of input field values when the user clicks Okay. Use this file with code like this: var p = showModalDialog("multiprompt.html", ["Enter 3D point coordinates", "x", "y", "z"], "dialogwidth:400; dialogheight:300; resizable:yes"); --> <form> <fieldsetid="fields"></fieldset><!-- Dialog body filled in by script below --> <divstyle="text-align:center"><!-- Buttons to dismiss the dialog --> <buttononclick="okay()">Okay</button><!-- Set return value and close --> <buttononclick="cancel()">Cancel</button><!-- Close with no return value --> </div> <script> // Create the HTML for the dialog body and display it in the fieldset var args = dialogArguments; var text = "<legend>" + args[0] + "</legend>"; for (var i = 1; i < args.length; i++) text += "<label>" + args[i] + ": <input id='f" + i + "'></label><br>"; document.getElementById("fields").innerHTML = text; // Close the dialog without setting a return value functioncancel() { window.close(); } // Read the input field values and set a return value, then close functionokay() { window.returnValue = []; // Return an array for (var i = 1; i < args.length; i++) // Set elements from input fields window.returnValue[i - 1] = document.getElementById("f" + i).value; window.close(); // Close the dialog. This makes showModalDialog() return. } </script> </form>