尝试创建一个书签,询问/提示用户输入URL参数,并将指定的URL参数的值作为警报消息输出。
例如,给定一个URL,如果您传入名称,则书签应输出“Taylor”。如果传入accountID,则应输出“123456789”,如果通过电子邮件,则应输出“tay@tc.com”。
示例:
http://www.google.com/name=Taylor&accountID=123456789&email=jdoe@tc.com
规则:
不确定如何开始这个。我应该从尝试构建搜索功能或类似的东西开始:
(function (url, options) {
window.open(
encodeURIComponent(url),
options
);
}('http://www.google.com/name=Taylor&accountID=123456789&email=jdoe@tc.com','name, accountID, email'));
答案 0 :(得分:0)
有很多书签创建者。只是一个Google搜索而已。
( function() {
var i, param, pair,
urlParams = window.location.search.substring( 1 ),
atts = urlParams.split( '&' );
if ( atts[0] !== '' ) {
param = prompt( 'Enter desired URL parameter:' );
for ( i = 0; i < atts.length; i++ ) {
pair = atts[ i ].split( '=' );
if ( pair[0] === param ) {
alert( pair[1] );
return false;
}
}
alert( 'URL parameter "' + param + '" does not exist.' );
} else {
alert( 'This URL does not contain any URL parameters.' );
}
return false;
} )();
书签代码:
javascript:(function()%7B(%20function()%20%7Bvar%20i%2C%20param%2C%20pair%2CurlParams%20%3D%20window.location.search.substring(%201%20) %2Catts%20%20%20%20%20%20%3D%20urlParams.split(%20'%26'%20)%3Bif%20(%20atts%5B0%5D%20!%3D%3D%20 ''%20)%20%7Bparam%20%3D%20prompt(%20'Enter%20desired%20URL%20parameter%3A'%20)%3Bfor%20(%20i%20%3D%200%3B%20i% 20%3C%20atts.length%3B%20i%2B%2B%20)%20%7Bpair%20%3D%20atts%5B%20i%20%5D.split(%20'%3D'%20)%3Bif %20(%20pair%5B0%5D%20%3D%3D%3D%20param%20)%20%7Balert(%20pair%5B1%5D%20)%3Breturn%20false%3B%7D%7Dalert(%20' URL%20parameter%20%22'%20%2B%20param%20%2B%20'%22%20does%20not%20exist。'%20)%3B%7D%20else%20%7Balert(%20'This% 20URL%20does%20not%20contain%20any%20URL%20parameters。'%20)%3B%7Dreturn%20false%3B%7D%20)()%7D)()