问题
I'm using the linkedin JS-API
This example
I'm accepting the company name from the user. So, I want to build the <script>
part of the above example programmatically. This is what I've done so far,
function companySearch()
{
var companyName = document.getElementById("companyName").value;
var searchHTML = "Search Results : ";
searchHTML = searchHTML + "<p> The company details are as follows";
searchHTML = searchHTML + "<script type=\"IN/CompanyProfile\" data-id=\"linkedin\" data-format=\"hover\"></script>";
searchHTML = searchHTML + "</p>";
document.getElementById("companyResult").innerHTML = searchHTML;
}
Markup:
<script type="IN/Login"></script>
<h1>
LINKEDIN COMPANY SEARCH
</h1>
<table>
<tr>
<td>
Company Name :
</td>
<td>
<input type="Text" id="companyName" value=""/>
</td>
<td>
</td>
<td>
<input type="button" value="Search LinkedIn" onclick="companySearch()"/>
</td>
</tr>
</table>
<div id="companyResult">
</div>
Output:

What is going wrong here?
回答1:
You need to break up your script tags so the parser doesn't see them:
searchHTML = searchHTML + "<scr" + "ipt type=\"IN/CompanyProfile\" data-id=\"linkedin\" data-format=\"hover\"></scr" + "ipt>";
来源:https://stackoverflow.com/questions/8187413/add-javascript-programmatically