在java中打印列表的内容

时间:2016-10-31 22:31:03

标签: javascript string list substring

尝试编写javascript,从下面的网址打印“username = john01”和“password = password123”。目前它什么都没打印......

index.html?username = john01& password = password123& lt = _c1F9F2B16-96B3-9D7B-CC19-D22A43D4FCA1_kA54D6D0E-881B-2792-22D3-B857150B1EFC& _eventId = submit& submit = Sign + In

<html>
<body>
<p> This code is for educational purposes only, and is not to be used with malicious intent. </p>
<script>
  string = window.location.search;
  content = string.split("");
  usernameStart = 1;
  usernameEnd = content.indexOf("&",0);
  username = content.substring(usernameStart,usernameEnd));
  document.write("Your username is " + username);
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

var url = 'index.html?username=john01&password=password123&lt=_c1F9F2B16-96B3-9D7B-CC19-D22A43D4FCA1_kA54D6D0E';

var username = url.match(/username=([^&]*)/)[1];
var password = url.match(/password=([^&]*)/)[1];

document.getElementById('userinfo').innerHTML = 'Your username is ' + username + '<br/>And your password is ' + password;

您可以直接在JSFiddle

上试用

如果将'url'替换为'window.location'并删除第一行,则应该从该URL开始。