• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1523
  • Last Modified:

Javascript - Insert Text

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...

Regards,
mnewm
0
mnewm
Asked:
mnewm
2 Solutions
 
nizsmoDeveloperCommented:
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?
0
 
mnewmAuthor Commented:
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?

Thanks
0
 
nizsmoDeveloperCommented:
Sorry not that I am aware of.

However, if your problem is determining which includes page to include for your page, you can use:
$_SERVER['PHP_SELF']

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")) {
      include("thisfile.php"):
      }
if (stristr(htmlentities($_SERVER['PHP_SELF']), "file2.php")) {
      include("thatfile.php"):
      }


I hope I understood you correctly.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mnewmAuthor Commented:
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...
0
 
nizsmoDeveloperCommented:
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.
0
 
mnewmAuthor Commented:
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!! :-)
0
 
nizsmoDeveloperCommented:
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?
0
 
mnewmAuthor Commented:
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....
0
 
gops1Commented:
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" "http://www.w3.org/TR/html4/strict.dtd">
<html>
      <head>
            <title>Script Demo Gops&reg;</title>
            <script language="javascript">
                  window.onload=function(){
                        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");
                        sp.appendChild(txt);
                        document.insertBefore(sp,p);
                        alert("After Inserting the first Tag in this document\n"+window.document.childNodes[0].tagName)
                  }
            </script>
      </head>
      <body>

      </body>
</html>
0
 
mnewmAuthor Commented:
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.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now