Javascript - Insert Text

Posted on 2007-10-21
Last Modified: 2008-02-02
Is it possible to specify what line number javascript writes text to?

I want to be able to insert text above the DOC tag, but it keeps sitting in the head tag...

Question by:mnewm
    LVL 21

    Expert Comment

    Have you tried placing your code above the doc tag and calling the write() method?
    Anyway I don't think this is very conventional, any reason why you want to do this?

    Author Comment

    I have a separate JS file and I want to write PHP includes to the page header.

    I can get the document to write to the head tags, but can I specify what location to write to? Such as line numbers?

    LVL 21

    Accepted Solution

    Sorry not that I am aware of.

    However, if your problem is determining which includes page to include for your page, you can use:

    to determine which filename.php you are curently in. Furthermore you can compare in PHP to assign their appropriate include pages by using this:

    if (stristr(htmlentities($_SERVER['PHP_SELF']), "file1.php")) {
    if (stristr(htmlentities($_SERVER['PHP_SELF']), "file2.php")) {

    I hope I understood you correctly.

    Author Comment

    It's not determined on the page name, but the user action (pressing a button). The file which is called is purely javascript, so PHP can't be used.

    Is there anyway I can look for the <!DOC tag and insert before this?

    Help is greatly appreciated! So thanks for this...
    LVL 21

    Expert Comment

    Ahh if it is purely javascript, then you can use AJAX to communicate with the PHP file to pass in the parameter (when the button is clicked) which would then trigger which include file to use.

    I think this would be a more conventional way of doing it rather than using purely javascript.

    Author Comment

    I would prefer to do this with javascript as to work with all other coding -

    As it stands:
          var eHead = dreamweaver.getDocumentDOM().getElementsByTagName("TOP");
          var sHtml = "";
          eHead[0].innerHTML += generateJavaScript();
          sHtml += '<?php $redirectURL = "' + document.redirectURL.value + '";\ninclude("/includes/newsletter.php");?>';
          return sHtml;

    This works fine if I manually add in the <TOP> tags at the top of the document. Sorry - the actual file that will be written to is PHP, but the script is JS - javascript is preferred!! :-)
    LVL 21

    Expert Comment

    How about inserting a <span id="test"></span> before the doc tag and then updating using the innerHTML value (document.getElementByID('test').innerHTML = "your include file statement";)?

    Will that work?

    Author Comment

    It will work the same as when using the TOP head tag, but I want it to be inserted without any extra coding being added-
    I've got so far as learning the DOC tag is called the prologue, if that helps any? I'm stuck anyway....
    LVL 17

    Assisted Solution

    Using Javascript DOM this is invalid to insert something before the DOCTYPE tag. Any browser that follows W3C standards strictly, probably will not allow this. May be in IE browser you can try this:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "">
                <title>Script Demo Gops&reg;</title>
                <script language="javascript">
                            alert("Before Inserting the first Tag in this document\n"+window.document.childNodes[0].tagName)
                            var p=document.getElementsByTagName('!')[0];
                            var sp=document.createElement('span');
                            var txt=document.createTextNode("Hello");
                            alert("After Inserting the first Tag in this document\n"+window.document.childNodes[0].tagName)


    Author Comment

    Thanks for the suggestion, but no luck.

    The script being inserted about the DOC tag is PHP header information - I can't see this being a problem with W3C...the above script is adding some script to the body text, but not the specified text...more the styles in the javascript file.
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now