Script will load but not through SRC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ojsimon
    New Member
    • May 2007
    • 59

    Script will load but not through SRC

    Hi
    this javascript is generated by my code
    Code:
    document.writeln('<head><style>a{color: white; font-size:10px}a:visited{color: white; font-size: 10px;}</style></head><table><tr><tr BACKGROUND="http://localhost:8888/sitewidget/app/webroot/img/border.png"><th><font face="Verdana, Arial" size="4" color="white">Widget</font></th></tr><tr><td><style type="text/css"><!--#frame1 {    width: 400px;    height: 200px;    border: none;    background: white;}--></style><iframe src="/sitewidget/index.php/widget/show/aHR0cDovL3d3dy5iYmMuY28udWsvbG9uZG9uL25ld3Mv/156" name="frame1" id="frame1" scrolling="auto" frameborder="0"></iframe>');</td><tr><tr BACKGROUND="http://localhost:8888/sitewidget/app/webroot/img/border.png"><th><font face="Verdana, Arial" size="20px" color="white" vlink="white"><a href="/sitewidget/index.php/grab/check/aHR0cDovL3d3dy5iYmMuY28udWsvbG9uZG9uL25ld3Mv/156" target="_blank">Get this widget</a></font> <font face="Verdana, Arial" size="2px" color="white"> |</font> <font face="Verdana, Arial" size="20px" color="white" vlink="white"><a href="http://www.websnips.com" target="_blank">Make a widget out of any site</a></font></th></tr></tr></table>'<!-- 0.1254s -->
    I am trying to run it like this
    Code:
    <html>
    <SCRIPT LANGUAGE="JavaScript" SRC="js.html">
    </SCRIPT>
    </html>
    I do not know why it is not running as an external file when it is running when you run the file directly?

    Thanks
    Last edited by acoder; Oct 12 '08, 07:09 PM. Reason: fixed code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    The src needs to be for a JavaScript file, not an HTML one.

    The attribute language is deprecated - set the type attribute instead to "text/javascript".

    The HTML document should have a body and a valid doctype.

    Not all of these should be the cause, but it's good practice anyway.

    Comment

    • ojsimon
      New Member
      • May 2007
      • 59

      #3
      Originally posted by acoder
      The src needs to be for a JavaScript file, not an HTML one.

      The attribute language is deprecated - set the type attribute instead to "text/javascript".

      The HTML document should have a body and a valid doctype.

      Not all of these should be the cause, but it's good practice anyway.
      I thought the file i am trying to src was javascript, how do i change this to javascript?

      Thanks

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Change the extension to .js.

        The script shouldn't really be writing the head content too. Try putting the head section in the HTML page and use the file to write the body content.

        Comment

        • Rsmastermind
          New Member
          • Sep 2008
          • 93

          #5
          In the header tag of the html file include this peice of code


          Code:
          <head><script type="text/javascript"
          	src="Relative path where your  file is located from the current directory"></script></head>
          Now change the extension properly from .html to .js be carefull while changing the extension sometimes in windows if you will change the extension like in this case the most generic error is, it will show the file.js on screen but actually it will be file.js.html.

          Comment

          • ojsimon
            New Member
            • May 2007
            • 59

            #6
            Originally posted by Rsmastermind
            In the header tag of the html file include this peice of code


            Code:
            <head><script type="text/javascript"
            	src="Relative path where your  file is located from the current directory"></script></head>
            Now change the extension properly from .html to .js be carefull while changing the extension sometimes in windows if you will change the extension like in this case the most generic error is, it will show the file.js on screen but actually it will be file.js.html.
            Thanks, the only problem with this is that it is infact an automated html page i changed the parameters to make it js. Also i was only using the path to the file as an example the actual path has no .extention is more like http://doman.comindex. php/grab/check/aHR0cDovL2dvb2d sZS5jb20=/40 with no html or js extension. Can i work with this without majorly edditing the php side of things which would be a major upheaval?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              send a javascript header in the returning file.
              [PHP]// in php you'd write on top
              header("content-type: text/javascript");[/PHP]
              regards

              Comment

              • rnd me
                Recognized Expert Contributor
                • Jun 2007
                • 427

                #8
                Originally posted by Dormilich
                send a javascript header in the returning file.
                [PHP]// in php you'd write on top
                header("content-type: text/javascript");[/PHP]
                regards
                this shouldn't matter. the .src of a script tag can be any mimetype, any extention, etc.

                it just has to actually be javascript to work. i serve all my .js files as HTML, which lets me view and edit them in browser. i have only seen one compatibility problem with an old version of opera in XHTML strict, but it works with recent releases.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  That's true, though, to avoid confusion, I'd avoid the .html extension unless it's obvious that it's a js file (with proper naming).

                  ojsimon, give an example of generated JavaScript which doesn't work.

                  Comment

                  • ojsimon
                    New Member
                    • May 2007
                    • 59

                    #10
                    i did post an example its the very first code section on my first post of this thread.

                    Thanks

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Is there no chance you could add line breaks to your code using separate document.writel n() statements for each line? Though it shouldn't make much difference, you're missing <body> tags.

                      Comment

                      • ojsimon
                        New Member
                        • May 2007
                        • 59

                        #12
                        Originally posted by acoder
                        Is there no chance you could add line breaks to your code using separate document.writel n() statements for each line? Though it shouldn't make much difference, you're missing <body> tags.
                        no it didnt make much difference but thanks, anyone else?

                        Thanks

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          When you load the page, are there any errors?

                          If possible, what you should do is only write the body content, at least for validation purposes. So, in your HTML file, have the head content in the file together with a body tag and then within the body tag load the JavaScript file:
                          Code:
                          <html>
                          <head>
                          ...
                          </head>
                          <body>
                          <script type="text/javascript" src="somefile.php"></script>
                          </body>
                          </html>

                          Comment

                          Working...