Trong bài viết hướng dẫn này, tác giả giới thiệu với bạn hai giải pháp JavaScript để thực hiện việc lấy các tham số trong đường dẫn URL hiện hành. Các giải pháp này rất đơn giản và dễ dàng để sử dụng.
- Demo
- Phóng to
- Tải lại
- Cửa sổ mới
Miễn phí web hosting 1 năm đầu tại iPage
Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?
Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.
Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
I found 2 simple solutions to reading GET variables with JavaScript.
They use either 'document.location' or 'window.location.href' to ready the URL and split it.
The first example I found here:
01.
function
getURLVar(urlVarName) {
02.
//divide the URL in half at the '?'
03.
var
urlHalves = String(document.location).split(
'?'
);
04.
var
urlVarValue =
''
;
05.
if
(urlHalves[1])
06.
{
07.
//load all the name/value pairs into an array
08.
var
urlVars = urlHalves[1].split(
'&'
);
09.
//loop over the list, and find the specified url variable
10.
for
(i=0; i<=(urlVars.length); i++)
11.
{
12.
if
(urlVars[i])
13.
{
14.
//load the name/value pair into an array
15.
var
urlVarPair = urlVars[i].split(
'='
);
16.
if
(urlVarPair[0] && urlVarPair[0] == urlVarName)
17.
{
18.
//I found a variable that matches, load it's value into the return variable
19.
urlVarValue = urlVarPair[1];
20.
}
21.
}
22.
}
23.
}
24.
return
urlVarValue;
25.
}
26.
27.
document.write(
'name = '
+ getURLVar(
'name'
));
The 2nd I found here. This one is a bit more simple.
01.
function
getUrlVars()
02.
{
03.
var
vars = [], hash;
04.
var
hashes = window.location.href.slice(window.location.href.indexOf(
'?'
) + 1).split(
'&'
);
05.
for
(
var
i = 0; i < hashes.length; i++)
06.
{
07.
hash = hashes[i].split(
'='
);
08.
vars.push(hash[0]);
09.
vars[hash[0]] = hash[1];
10.
}
11.
return
vars;
12.
}
13.
14.
var
getvars = getUrlVars();
15.
16.
document.write(
'name = '
+ getvars[
'name'
]);
I don't really like this solution because you get 'undefined' if you select a variable that does not exist. But that may be what you want to see if a variable is present in the URL.
I simplified the first example using the slice/split idea from the 2nd example and came up with a better solution:
01.
function
getURLVar(urlVarName) {
02.
var
urlVars = window.location.href.slice(window.location.href.indexOf(
'?'
) + 1).split(
'&'
);
03.
for
(i=0; i<=(urlVars.length); i++)
04.
{
05.
if
(urlVars[i])
06.
{
07.
var
urlVarPair = urlVars[i].split(
'='
);
08.
if
(urlVarPair[0] && urlVarPair[0] == urlVarName)
09.
{
10.
return
urlVarPair[1];
11.
}
12.
}
13.
}
14.
return
""
;
// Remove this if you want it to return 'undefined' when a variable does not exist in the URL.
15.
}
16.
17.
document.write(
'name = '
+ getURLVar(
'name'
));
- Lượt gửi (0)
- Mới
Save up to 630$ when buy new iPhone 15
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language
Dall-E-OpenAI.com
Generate creative images automatically with AI
AIVideo-App.com
Render creative video automatically with AI
Phản hồi