How to read GET data from a URL using JavaScript?
Please see this, more current solution before using a custom parsing function like below, or a 3rd party library.
The a code below works and is still useful in situations where URLSearchParams
is not available, but it was written in a time when there was no native solution available in JavaScript. In modern browsers or Node.js, prefer to use the built-in functionality.
function parseURLParams(url) {
var queryStart = url.indexOf("?") + 1,
queryEnd = url.indexOf("#") + 1 || url.length + 1,
query = url.slice(queryStart, queryEnd - 1),
pairs = query.replace(/\+/g, " ").split("&"),
parms = {}, i, n, v, nv;
if (query === url || query === "") return;
for (i = 0; i < pairs.length; i++) {
nv = pairs[i].split("=", 2);
n = decodeURIComponent(nv[0]);
v = decodeURIComponent(nv[1]);
if (!parms.hasOwnProperty(n)) parms[n] = [];
parms[n].push(nv.length === 2 ? v : null);
}
return parms;
}
Use as follows:
var urlString = "http://www.example.com/bar?a=a+a&b%20b=b&c=1&c=2&d#hash";
urlParams = parseURLParams(urlString);
which returns a an object like this:
{
"a" : ["a a"], /* param values are always returned as arrays */
"b b": ["b"], /* param names can have special chars as well */
"c" : ["1", "2"] /* an URL param can occur multiple times! */
"d" : [null] /* parameters without values are set to null */
}
So
parseURLParams("www.mints.com?name=something")
gives
{name: ["something"]}
EDIT: The original version of this answer used a regex-based approach to URL-parsing. It used a shorter function, but the approach was flawed and I replaced it with a proper parser.
How to get data request from link url in javascript?
You are making a cross-origin http request as you are requesting data from a domain http://gateway.fpts.com.vn
which is different form that of your html file shown in question, say it http://www.requesting-server.com
To allow cross-origin resource sharing, you should set following header on the requested server http://gateway.fpts.com.vn
.
To allow requests from any domain, use *
as a wildcard (less secure):
Access-Control-Allow-Origin: *
or
To allow requests from a particular domain:
Access-Control-Allow-Origin: http://www.requesting-server.com
You can also check this post which addresses the same issue.
get data from url
Since you're in a frame, if you need to get the href from the main window, do this:
var href = window.top.location.href;
Then process it.
Get the data-url correctly using javascript function
Try
var url = $(this).attr('data-url');
You are getting the attribue value
by className
and multiple elements
have same class
so the attribute value
for first element
found in dom
is being returned.
So you need to get the value
of element
which is clicked
and you can use this
to get the value
of clicked element
.
Read data from JSON Url using plain javascript and display it in HTML
You can use ES6 fetch API to load the request data and then render it.
fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(myJson);
});
Here's a tutorial on fetch API: https://scotch.io/tutorials/how-to-use-the-javascript-fetch-api-to-get-data
Fetch is now supported in all modern browsers: https://caniuse.com/#feat=fetch
HTML - read .txt file from URL location in javascript
this code may help you:
function getText(){ // read text from URL location var request = new XMLHttpRequest(); request.open('GET', 'http://www.puzzlers.org/pub/wordlists/pocket.txt', true); request.send(null); request.onreadystatechange = function () { if (request.readyState === 4 && request.status === 200) { var type = request.getResponseHeader('Content-Type'); if (type.indexOf("text") !== 1) { return request.responseText; } } }}function populateTables(){ var outer_text = getText(); outer_text = outer_text.split('\n'); // you can adjust the manner of parsing the received file (regexp) var tableHP = document.getElementById("tHP");// Create an empty <tr> element and add it to the 1st position of the table: var row = tableHP.insertRow(tableHP.rows.length);// Insert new cells (<td> elements) at the 1st and 2nd position of the "new" <tr> element: var cell1 = row.insertCell(0); var cell2 = row.insertCell(1);
// Add some text to the new cells: cell1.innerHTML = outer_text[0]; cell2.innerHTML = outer_text[1];}
Related Topics
This.Setstate Isn't Merging States as I Would Expect
Detect If Browser Tab Is Active or User Has Switched Away
Getting How Many People Are in a Chat Room in Socket.Io
How Dangerous Is It in JavaScript, Really, to Assume Undefined Is Not Overwritten
When to Use the Double Not (!!) Operator in JavaScript
This.Setstate Isn't Merging States as I Would Expect
Google Chromecast Sender Error If Chromecast Extension Is Not Installed or Using Incognito
How to Create a Session Using JavaScript
How to Resolve Typeerror: Cannot Convert Undefined or Null to Object
Es6 Modules Implementation, How to Load a JSON File
How to Block Users from Closing a Window in JavaScript
Package.JSON' Is Not Under 'Rootdir'
[Vue Warn]: Property or Method Is Not Defined on the Instance But Referenced During Render
Executing JavaScript Without a Browser