Return HTML content as a string, given URL. Javascript Function
you need to return when the readystate==4 e.g.
function httpGet(theUrl)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
return xmlhttp.responseText;
}
}
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();
}
Return HTML content as a string, given URL Javascript/Jquery Function?
I think, the question is "How can I get the URL (href property) of a link in the page using the tag content ( the content between <a> & </a> ) ".
You can use something like this:
function getLinkByTagContent(tagContent) {
var docLink;
for(var ii = 0; ii < document.links.length ; ii++) {
docLink = document.links[ii]
if( docLink.text === tagContent ) {
return docLink.href;
}
}
}
getting a string instead of html returned from function in React
When returning JSX in a function, you should just plainly return the JSX element you want. So you'd write return <div/>
instead of return '<div/>'
. So:
return '<img src="https:' + myUrl + '" />'
Should be
return <img src={"https:" + myUrl} />
Otherwise you'd return a string instead of JSX. Same in the other return statement, which should be:
return <video src={"https:" + myUrl}></video>
How to return HTML in javascript whilst including a variable i created?
As @Cid mentioned in the comments, using document.createElement()
is a good idea.
<!DOCTYPE html>
<html>
<body>
</body>
<script>
const div = document.createElement("div"); // create a div
const h1 = document.createElement("h1"); // create a h1
const img = document.createElement("img"); // create an image tag
const p = document.createElement("p"); // create a paragraph
div.classList.add("hello"); // assign "hello" css class
div.id = "hi"
h1.classList.add("header-1"); // assign "header" css class
p.classList.add("paragraph-1"); // assign "paragraph-1" css class
div.appendChild(h1); // nest h1 inside div
div.appendChild(img); // nest img inside div
div.appendChild(p); // nest p inside div
document.body.appendChild(div); // add div to current document
</script>
</html>
Related Topics
What Does "./" (Dot Slash) Refer to in Terms of an HTML File Path Location
Get File Size, Image Width and Height Before Upload
Disable Pasting Text into HTML Form
How to Get Selected Text from a Textbox Control With JavaScript
Html5 Canvas Resize (Downscale) Image High Quality
How to Dynamically Change a Web Page'S Title
How to Append Text to a '≪Div≫'
Reading File Contents on the Client-Side in JavaScript in Various Browsers
Make Iframe Automatically Adjust Height According to the Contents Without Using Scrollbar
When to Use Functional Setstate
Do Html5 Custom Data Attributes "Work" in Ie 6
Navbar Dropdown (Collapse) Is Not Working in Bootstrap 5
Changing CSS Values With JavaScript
Which Browsers Support ≪Script Async="Async" /≫
When Do Items in Html5 Local Storage Expire