//<![CDATA[
// external.js
var doc, bod, htm, post, C, E, T; // for use on other loads
addEventListener('load', function(){ // load start
// I threw in a few goodies to study - it will help you later
doc = document; bod = doc.body; htm = doc.documentElement;
function phpEncode(obj){
var r = [];
if(obj instanceof Array){
for(var i=0,l=obj.length; i<l; i++){
r.push(phpEncode(obj[i]));
}
return '%5B'+r.join(',')+'%5D';
}
else if(typeof obj === 'object' && obj){
for(var i in obj){
if(obj.hasOwnProperty(i)){
var v = obj[i], s;
if(typeof v === 'object' && v){
s = encodeURIComponent('"'+i.replace('"', '\\"')+'":')+phpEncode(v);
}
else{
v = typeof v === 'string' ? '"'+v.replace('"', '\"')+'"' : v;
s = encodeURIComponent('"'+i.replace('"', '\\"')+'":'+v);
}
r.push(s);
}
}
return '%7B'+r.join(',')+'%7D';
}
else{
r = typeof obj === 'string' ? '"'+obj.replace('"', '\\"')+'"' : obj;
return ''+r;
}
}
function phpAccept(url){
return eval('('+decodeURIComponent(url)+')');
}
post = function(send, where, success, context){
var x = new XMLHttpRequest;
var c = context || this;
x.open('POST', where); x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.onreadystatechange = function(){
if(x.readyState === 4 && x.status === 200){
if(success)success.call(c, phpAccept(x.responseText));
}
}
if(typeof send === 'object' && send && !(send instanceof Array)){
if(send instanceof FormData){
x.send(send);
}
else{
var r = [];
for(var p in send){
r.push(encodeURIComponent(p)+'='+phpEncode(send[p]));
}
x.send(r.join('&'));
}
}
else{
throw new Error('send must be an Object');
}
return x;
}
C = function(tag){
return doc.createElement(tag);
}
E = function(id){
return doc.getElementById(id);
}
T = function(tag){ // returns an Array of Elements by tag name
return doc.getElementsByTagName(tag);
}
E('form').addEventListener('submit', function(ev){
ev.preventDefault();
});
var page = E('page'), err = E('error');
var pr = 'Positive Integer Required';
function gF(){
var val = page.value;
if(val.match(/\d+/)){
location = 'yourPageHere.php?page='+val;
}
else{
err.innerHTML = pr;
}
}
E('go').addEventListener('click', gF);
page.addEventListener('keydown', function(ev){
if(ev.keyCode === 13)gF();
});
page.addEventListener('keyup', function(){
err.innerHTML = page.value.match(/^[1-9]+[0-9]*$/) ? '' : pr;
});
}); // end load
/* external.css */
html,body{
padding:0; margin:0;
}
.main{
width:980px; margin:0 auto;
}
#contain{
width:116px; background:#070; color:#fff; padding:10px;
}
#contain>label{
display:block; float:left; width:40px;
}
#page{
width:30px; float:left;
}
#go{
display:block; width:40px; margin:0 auto;
}
#error{
width:136px; color:#900; text-align:center;
}
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<link type='text/css' rel='stylesheet' href='external.css' />
<script type='text/javascript' src='external.js'></script>
</head>
<body>
<div class='main'>
<form id='form'>
<div id='contain'>
<label for='page'>Page:</label><input id='page' name='page' type='text' value='1' />
<input type='button' id='go' value='GO' />
</div>
</form>
<div id='error'></div>
</div>
</body>
</html>
在JavaScript中改變位置就像'location ='someURL.php?prop1 = val1''一樣簡單。 – PHPglue
'$ _POST ['nrpng']'不會在第一次加載時存在,它只會在表單發佈之後才存在 – nogad
@PHPglue請你多解釋一下,我在這裏有點新鮮。 –