如何使用Javascript标记从HTML标记中提取URL参数? [英] How do you extract a URL argument from an HTML tag by using Javascript tokens?

查看:110
本文介绍了如何使用Javascript标记从HTML标记中提取URL参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个字符串,我想提取& key = f430a2c1之后的所有内容,直到&

Here is a string I want to extract everything after &key=f430a2c1 until &

有没有办法访问这些数据,然后提取密钥?

Is there a way to access this data, then extract the key?

<img src="img/index.php?key=f430a2c1&rand=736920" alt=

这个字符串存在于文档中,我可能需要一些函数来从src =区域获取它。 / b>

This string exists within the document, and I may need some function to grab it from the src="" area.

推荐答案

我猜你想要什么,但也许是这样?

I am guessing as to what you want, but maybe this?

var images = document.getElementsByTagName("img"),
    wanted;

if (images.length) {
    wanted = images[0].src;
    if (wanted) {
        wanted = wanted.split("?");
        if (wanted.length >= 2 && wanted[1].indexOf("&") !== -1) {
            wanted = wanted[1].split("&")[0];
        }
    }
}

if (typeof wanted !== "string") {
    wanted = "";
}

console.log(wanted);

输出

key=f430a2c1 

jsfiddle

这篇关于如何使用Javascript标记从HTML标记中提取URL参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆