在“中使用JavaScript”;按钮“;网页上的自我识别

在“中使用JavaScript”;按钮“;网页上的自我识别,javascript,php,html,Javascript,Php,Html,我有一个网站,我在其中放置了文章内部和标题/正文/页脚外部的特定按钮。我计划做的是,当用户单击按钮时,JavaScript代码应该找出按钮是位于文章内部还是外部,并使用PHP的$\u GET函数将信息发送到我的父网站。这是“儿童”网站上的代码 <!-- Button code --> <p id="test"></p> <a id ="webs" href= "" onclick="jams(this);document.getElementById('

我有一个网站,我在其中放置了文章内部和标题/正文/页脚外部的特定按钮。我计划做的是,当用户单击按钮时,JavaScript代码应该找出按钮是位于文章内部还是外部,并使用PHP的
$\u GET
函数将信息发送到我的父网站。这是“儿童”网站上的代码

<!-- Button code -->
<p id="test"></p>
<a id ="webs" href= "" onclick="jams(this);document.getElementById('test').frameBorder=0"; target="test"><button>Click me!</button> </a>
因此,当用户单击网页上的按钮时,该按钮将调用jams脚本,它识别按钮在网页上的位置,并将网站名称或文章URL发送到one.phptwo.php

当按钮位于文章中时,脚本工作正常,
href=

http://www.parentwebsite.com/two.php?id=http://child-articles-url.com/ 
但是当它在文章之外时失败,
href

http://www.parentwebsite.com/two.php?id=http://www.parentwebsite.com/one.php?id=http://www.child.com/
它需要给予的只是:

http://www.parentwebsite.com/one.php?id=http://www.child.com/
另外,我在处理这个问题时使用了chrome的调试器


提前感谢!:)

我试图解决你的问题

index.php

<!DOCTYPE html>
<html>
<head>
    <title>Using JavaScript in “button” self recognition on webpage</title>
</head>
<body>
<!-- Your HTML CODE -->
<div class="aricle">
    <h2> Article Starts </h2>
        <a href="#" onclick="link(1);">This is inside Article</a>
    <h2> Article Ends </h2>
</div>
<br />
<a href="#" onclick="link(2);">This is inside Article</a>

<!-- Your JS CODE -->

<script type="text/javascript">
    function link(button) {

        if (button == 1) {
            //alert("button inside article");
            window.location.href='http://localhost/stackTest/one.php';
        } else if (button == 2) {
            //alert("button outside article");
            window.location.href='http://localhost/stackTest/two.php';
        }
    }
</script>
</body>
</html>
<h2>This is One</h2>
<h1>This is Two</h2>

JavaScript在网页“按钮”自我识别中的应用
文章开头
文章结尾

功能链接(按钮){ 如果(按钮==1){ //警告(“物品内的按钮”); window.location.href=http://localhost/stackTest/one.php'; }否则如果(按钮==2){ //警告(“物品外的按钮”); window.location.href=http://localhost/stackTest/two.php'; } }
one.php

<!DOCTYPE html>
<html>
<head>
    <title>Using JavaScript in “button” self recognition on webpage</title>
</head>
<body>
<!-- Your HTML CODE -->
<div class="aricle">
    <h2> Article Starts </h2>
        <a href="#" onclick="link(1);">This is inside Article</a>
    <h2> Article Ends </h2>
</div>
<br />
<a href="#" onclick="link(2);">This is inside Article</a>

<!-- Your JS CODE -->

<script type="text/javascript">
    function link(button) {

        if (button == 1) {
            //alert("button inside article");
            window.location.href='http://localhost/stackTest/one.php';
        } else if (button == 2) {
            //alert("button outside article");
            window.location.href='http://localhost/stackTest/two.php';
        }
    }
</script>
</body>
</html>
<h2>This is One</h2>
<h1>This is Two</h2>
这是一个
two.php

<!DOCTYPE html>
<html>
<head>
    <title>Using JavaScript in “button” self recognition on webpage</title>
</head>
<body>
<!-- Your HTML CODE -->
<div class="aricle">
    <h2> Article Starts </h2>
        <a href="#" onclick="link(1);">This is inside Article</a>
    <h2> Article Ends </h2>
</div>
<br />
<a href="#" onclick="link(2);">This is inside Article</a>

<!-- Your JS CODE -->

<script type="text/javascript">
    function link(button) {

        if (button == 1) {
            //alert("button inside article");
            window.location.href='http://localhost/stackTest/one.php';
        } else if (button == 2) {
            //alert("button outside article");
            window.location.href='http://localhost/stackTest/two.php';
        }
    }
</script>
</body>
</html>
<h2>This is One</h2>
<h1>This is Two</h2>
这是两个
希望这能帮助您识别按钮。 您可以使用锚定(a)标记显示为按钮,而不是在
中使用
标记。
标记。
如果您使用的是Bootstrap,那么只需使用

的“Button”类,似乎您在设置URL时忘记了从事件处理程序返回,因此最后一次分配总是被执行。只需在第一个
origZ.href=链接后添加
return
while循环中的外观。链接中有一个按钮是非常奇怪的。相反,请执行
并设置链接样式,同时尝试
var spString=encodeURIComponent(window.location.href)
@Boynux在第一个
origZ.href=链接后添加
返回
one.php可以正常工作,但会出现新问题。。文章内部按钮的功能(单击时)检索到与文章外部按钮相同的数据。这意味着确定按钮位置的逻辑不正确。您可能需要调试JS逻辑,以找出它的错误。上面发布的JS代码语法不正确,因此我无法向您提供更多信息。为什么不干脆
window.location.href=http://localhost/stackTest/two.php';。assign很少使用,速度也很慢,谢谢。我测试了两个案例。。。分配实际上比较慢。