将参数传递给 << 内的链接a>仅使用 HTML 的 href 标签

发布于 2025-01-01 23:50:39 字数 1217 浏览 2 评论 0 原文

我决定重新提交这个问题,因为我可能不清楚我的问题和我的总体目标。

我目前正在处理一个仅使用 HTML 的网站。我在 URL 中传递了由 javascript 读取的变量,然后获取每个变量的参数并将其填充到 Google javascript 中。

JS 读取 URL:

function gup(name) { 
var params = {}; 
var parts = (window.location.search || '').split(/[&?]/); 
for (var i = 0; i < parts.length; ++i) { 
var eq = parts[i].indexOf('='); 
if (eq < 0) continue; 
params[decodeURIComponent(parts[i].substring(0, eq))] 
= decodeURIComponent(parts[i].substring(eq+1)); 
} 
return Object.hasOwnProperty.call(params, name) 
? params[name] : null; 
} 

要写入第二个 JS 的变量:

var keyValue1 = gup( 'pid1' ); 

写入的 Google JS 部分:

GA_googleAddAttr("pid1", keyValue1);

所有这些对我来说都工作得很好。这些参数被传递到 Google 广告中,但页面上仍然有几个没有附加“?pid1=”变量的链接。我希望能够执行以下操作之一:

  1. 从变量“pid1”中获取参数并将其传递到位于

    中的链接中。 a>标签。 (注意:我确实有多个变量,example.com/index.html?pid1=abc&pid2=def&pid3=ghi)

  2. 从 URL 中剥离,以 ? 开头以及此后的所有内容,并将其附加到一个链接或位于 < 中的所有链接。 a>页面上的标记。

  3. 从 URL 中剥离,以 ? 开头并且仅选择一组变量并将其附加到一个链接或位于 << 中的所有链接。 a>页面上的标记。

非常重要的一点是,我的 JS 知识仅限于提出问题、获取我所得到的信息并尝试从中找出答案,这就是我希望在这里做的事情。预先感谢您提供的任何帮助。

I decided to resubmit this question because I probably was not clear on my problem and my overall objective.

I am currently working with a website using only HTML. I have variables being passed in the URL that are read by a javascript and I then take the parameter for each variable and populate it into a Google javascript.

JS reading the URL:

function gup(name) { 
var params = {}; 
var parts = (window.location.search || '').split(/[&?]/); 
for (var i = 0; i < parts.length; ++i) { 
var eq = parts[i].indexOf('='); 
if (eq < 0) continue; 
params[decodeURIComponent(parts[i].substring(0, eq))] 
= decodeURIComponent(parts[i].substring(eq+1)); 
} 
return Object.hasOwnProperty.call(params, name) 
? params[name] : null; 
} 

The variable to be written to the second JS:

var keyValue1 = gup( 'pid1' ); 

The portion of the Google JS that is written to:

GA_googleAddAttr("pid1", keyValue1);

All of this has been working quite well for me. Those parameters are passed into Google ads but I still have several links on the page that do not have the '?pid1=' variable attached. I would like to be able to do one of the following:

  1. Take the parameter from the variable 'pid1' and pass it into a link located in a < a > tag. ( NOTE: I do have multiple variables, example.com/index.html?pid1=abc&pid2=def&pid3=ghi )

  2. Strip from the URL, starting with the ? and everything thereafter and attach it to one link or all links located in a < a > tag(s) on the page.

  3. Strip from the URL, starting with the ? and only a selected set of variable(s) and attach it to one link or all links located in a < a > tag(s) on the page.

Very important note, my JS knowledge is limited to asking questions and taking what I get and trying to figure it out from there, which is what I am hoping to do here. Thanks in advance for any help provided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-01-08 23:50:39

我不太确定 标签是否已经在页面上,但如果是:

var links = document.getElementsByTagName('a')
for(var i = 0; i < links.length; i++ ) {
     if(links[i].href === "http://foo.com/index.html?pid1=") {
         links[i].href = links[i].href + gup( 'pid1' );
     }

}

Im not really sure if the <a> tag is already on the page, but if it is:

var links = document.getElementsByTagName('a')
for(var i = 0; i < links.length; i++ ) {
     if(links[i].href === "http://foo.com/index.html?pid1=") {
         links[i].href = links[i].href + gup( 'pid1' );
     }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文