Get current URL in web browser
<script>
var url = window.location.href;
alert(url);
</script>
Get a timestamp in JavaScript
var timestamp = Math.floor(Date.now() / 1000);
alert(timestamp);
Ajax (注意不能跨域)
get:
<html>
<head>
<title>Test</title>
</head>
<body>
Test JavaScript.
<body>
<script>
var url = "http://localhost:8000/js";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('GET', url, true);
xhr.send(null);
</script>
</html>
post:
<html>
<head>
<title>Test</title>
</head>
<body>
Test JavaScript.
<body>
<script>
var url = "http://localhost:8000/js";
var params = "timestamp=3801480158&url=http://willowchen.com";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
alert(xhr.responseText);
}
}
xhr.open('POST', url, true);
xhr.send(params);
</script>
</html>