Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery/javascript从head中动态添加的script标记读取值_Javascript_Jquery - Fatal编程技术网

如何使用jquery/javascript从head中动态添加的script标记读取值

如何使用jquery/javascript从head中动态添加的script标记读取值,javascript,jquery,Javascript,Jquery,我有一个来自服务的脚本标记。我只是将其附加到html的头部部分 它会被追加,但问题是它有一些值,我需要在我的应用程序中访问这些值 <script> abc.header.value = "1"; abc.header.path = "/abc/def/ghi?isThirdParty=false&version=v123"; </script> abc.header.value=“1”; abc.header.path=“/abc/def/ghi?isTh

我有一个来自服务的脚本标记。我只是将其附加到html的头部部分

它会被追加,但问题是它有一些值,我需要在我的应用程序中访问这些值

<script>

abc.header.value = "1";
abc.header.path = "/abc/def/ghi?isThirdParty=false&version=v123";

</script>

abc.header.value=“1”;
abc.header.path=“/abc/def/ghi?isThirdParty=false&version=v123”;
一旦脚本添加到header,如果我尝试访问abc.header.value,它就会抛出未定义的值

如何从添加的脚本中访问值。还有一个全局对象,有没有一种方法可以将所有这些动态添加的值添加到该对象中

示例代码段: 我从ajax调用中获取脚本标记,并将其存储在变量中

var sampSnip = "<script> abc.header.value = "1"; abc.header.path = "/abc/def/ghi?isThirdParty=false&version=v123"; </script>";

$('head').append(sampSnip);
var sampSnip=“abc.header.value=”1”;abc.header.path=“/abc/def/ghi?isThirdParty=false&version=v123”;
$('head')。追加(sampSnip);

看看我刚刚为您编写的这个简单的登录表单

//HTML

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles/index.css"> 
    <script src="scripts/index.js"></script>
</head>
<body>
    <form>
        <input type="text" name="username" placeholder="username">
        <input type="password" name="password" placeholder="password">
        <input id='signin' type="button" value="Sign In">
    </form>
</body>
</html>

看看我刚刚为你写的这个简单的签到表

//HTML

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles/index.css"> 
    <script src="scripts/index.js"></script>
</head>
<body>
    <form>
        <input type="text" name="username" placeholder="username">
        <input type="password" name="password" placeholder="password">
        <input id='signin' type="button" value="Sign In">
    </form>
</body>
</html>

您能否显示您试图访问
abc.header.value
的位置?一些调试步骤-
console.log(abc)
然后
console.log(abc.header)
。查看这些对象中是否存在变量值。2.如果它们显示了对象,但是您的
路径
丢失,那么您正在尝试在分配之前访问它们。这取决于您如何插入这些“新”变量,发布创建scrip标记和值的代码,是的,肯定是向对象动态添加值的方法,但在此之前,这是“动态”的吗服务器通过AJAX调用生成数据?@JordanDavis:Yes脚本标记正在从AJAX调用加载。我加了一个样品snippet@M-感谢您的澄清,因此最佳做法是使用JSON.parse(e.target.response)来解析服务器的响应,而不是创建脚本标记并插入值,如果您希望这些值是全局的,您可以将其设置为变量,而不使用var关键字。示例-myData=JSON(e.target.response);然后是console.log(myData)。您能显示您试图访问
abc.header.value
的位置吗?一些调试步骤-
console.log(abc)
然后是
console.log(abc.header)
。查看这些对象中是否存在变量值。2.如果它们显示了对象,但是您的
路径
丢失,那么您正在尝试在分配之前访问它们。这取决于您如何插入这些“新”变量,发布创建scrip标记和值的代码,是的,肯定是向对象动态添加值的方法,但在此之前,这是“动态”的吗服务器通过AJAX调用生成数据?@JordanDavis:Yes脚本标记正在从AJAX调用加载。我加了一个样品snippet@M-感谢您的澄清,因此最佳做法是使用JSON.parse(e.target.response)来解析服务器的响应,而不是创建脚本标记并插入值,如果您希望这些值是全局的,您可以将其设置为变量,而不使用var关键字。示例-myData=JSON(e.target.response);然后是console.log(myData)。
<?php
echo json_encode($_POST['credentials']);
?>
body{
    margin: 0 !important;
    height: 100vh;
    width: 100vw;
    display: -webkit-flex;
    display: flex;
    text-align: center;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-flex-direction: column;
    flex-direction: column;
    background-color: #0B9AD8;
    -webkit-background-size: cover; 
    background-size: cover;
}
form{
    display: -webkit-flex;
    display: flex;
    -webkit-align-self: center;
    align-self: center;
    -webkit-flex-direction: column;
    flex-direction: column;
    padding: 1em;
    margin: 0 !important;
    background-color: #ECF0F1;
    -webkit-border-radius: .3em;
    border-radius: .3em;
    -webkit-box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
    box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
}
input{
    width:22em; 
    height: 3em;
    font-size: 1em;
    font-weight: lighter !important; /* FIREFOX */
    display: block;
    margin-bottom: .5em;
    outline: none;
    text-align: center;
    background-color: #fff; 
    border: 1px solid #d5dadc;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    padding: 0 !important; /* FIREFOX */
}
input:-webkit-autofill{
    -webkit-box-shadow: 0 0 0px 1000px white inset;
}
input[type="button"]{
    cursor: pointer;
    border: 1px solid #0089C4;
    color: #fff;
    background: #0089C4;
    margin-bottom: 0 !important;
}
input[type="button"]:hover{
    background: #0B9AD8;
}
input[type="button"]:active{
    -webkit-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15) inset;
    -moz-box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15) inset;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15) inset;
}