0

I'd like to embed an html tag in Javascript, between the script> tag

Thanks

2
  • why would you want to do that? Commented May 12, 2009 at 4:09
  • I'm need this to speed up a page load in asp.net I'm loading a script in asp.net before the page loads completely. When the page finally loads, I need to have access to the tag. Commented May 12, 2009 at 4:21

7 Answers 7

4

Hey maybe you're looking for jQuery.

$("p").text("<b>Some</b> new text.");

See embedded HTML tag.

Write Less, Do More

jQuery

Sign up to request clarification or add additional context in comments.

Comments

2

Is E4X what you're looking for? It allows you to embed XML/XHTML within your JavaScript, like this:

var someXml = <div><b>Some Text</b></div>;

I doubt that's what you need, but that's the only way you can do what you're asking. Also, I don't think it works in Internet Explorer. Scratch that, it only works in Firefox.

If that's not what you want, use document.write(), as suggested by others.


Edit: E4X is now deprecated and has been removed from newer versions of Firefox. Don't use it. You should use jQuery, as the answer below me suggests, or simply create the elements via document.createElement and friends and inject them into the document.

Comments

2

You can't. If you want the Javascript to write HTML, you'll have to use document.write().

Comments

1

If you write this inside your body tag then also you can access this using your javascript.

If yo want to check whether the document is ready or not then you can use JQuery

Comments

0

You can't do that. tags can contain just text (usually javascript).

Comments

0

document.write('

colour1: ' + colour1 + '
colour2: ' + colour2 + '

');

Comments

0

Using document.write(), though it is generally advised against the use of this method.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        document.write(`
        <html>
            <body>
                <p>html in script tag</p>
            <\/body>
        <\/html>
        `)
    </script>
</body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.