0

is it possible to have styles and scripts in the body of an Html page? On a forum I use I have realized that I can use other html in it using inline styling but can I put this in the body and use it?

Is it possible to put this code straight into the body of the html file?

<!-- Scripts and Themes for Syntax Highlighter, put in place on this thread by Fabian Cook (CyberPython) - Moderator -->
<!-- Core JS File -->
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" type="text/javascript"></script>
<!-- Java Theme -->
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/css/shBrushJava.js"></script>
<!-- Core Theme -->
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<!-- Core Style -->
<script src="http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js" type="text/javascript"></script>
<!-- Script to highlight syntax -->
<script type="text/javascript">SyntaxHighlighter.all()</script> 

And then use this as well

<pre class="brush:java">  
      package tutorial;
      import com.opensymphony.xwork2.ActionSupport;
      public class HelloWorld extends ActionSupport {
        private String name;
        public String getName() {
        return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String execute() {
            name = "Hello, " + name + "!"; 
            return SUCCESS;
        }
      }
</pre>
FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • Hi Smart, can you [edit] your question and further clarify what you are asking. It is not immediately clear to me what you're trying to accomplish. Thank you. – jamesmortensen Jun 24 '12 at 21:19
  • That's what I don't get. Did you try this? I'm confused about what you're asking, because on the surface, it seems to me that you could answer this in 5 minutes just trying it yourself. Once you try it and are stuck, then maybe come back and ask ;) If I misunderstand your question, that's fine, maybe you can clarify further, but if it's literally, can I do X, StackOverflow probably isn't the best place to ask. – jamesmortensen Jun 24 '12 at 21:25
  • School blocks typepad -.- so I can't test it myself. – FabianCook Jun 24 '12 at 21:27
  • That helps to know. (It's a good idea to include stuff like that in your question so people don't get the wrong idea about your intentions.) In that case, see matt3141's answer. This will help you get started. Good luck! :) – jamesmortensen Jun 24 '12 at 21:28
  • What other ideas could they get from this may I ask? The question is in bold.. – FabianCook Jun 24 '12 at 21:30
  • To clarify, questions you ask should show that you've done your own research on the topic. I'm sure this topic has been covered in numerous places on the Internet. The fact that you don't have a way to try it is helpful in determining your motivation for asking. Hope that helps. Good luck! – jamesmortensen Jun 24 '12 at 21:35
  • 1
    @jmort253, Just "trying", as you suggested, would be the wrong approach in this context: it can't give the definitive answer. It may work in your particular version of a particular browser, and still fail in many other cases. Or, it may even work in most browsers, while still being an invalid (nonconforming, deprecated etc.) technique likely to fail eventually/unpredictably. – Sz. May 03 '18 at 11:26

1 Answers1

1

Scripts included at any point but scripts may need to be included after html content in some cases. Styles should not be.

If you were wondering if you could directly enter the file contents, do it with script and style tags that have the same content as the linked files.

<script>
    //code...
</script>
<style>
    /* styles */
</style>
matt3141
  • 4,303
  • 1
  • 19
  • 24
  • So I can put style tags into the body? – FabianCook Jun 24 '12 at 21:24
  • 1
    @SmartLemon - Just to clarify, the `` tags won't work in the body, but embedded style will, as matt shows. You can also use AJAX to read the contents of the link CSS files and then generate embedded scripts with the CSS content. Hope this helps, and +1 on the answer. – jamesmortensen Jun 24 '12 at 21:29
  • I know how they work, just thought they were meant to be in the head, I knew script could go into the body. – FabianCook Jun 24 '12 at 21:29
  • 1
    One point, I'm not 100% sure, but ` – jamesmortensen Jun 24 '12 at 21:30
  • @jmost253 - these things are real easy to test. `` and ` – Alohci Jun 24 '12 at 21:38
  • @Alohci - Really? I seem to recall link tags not working for me in IE. Maybe it was because I added them dynamically, after the page loaded. You're right that it's easy to test, which is why my 2 cents is a comment and not an answer. :) – jamesmortensen Jun 24 '12 at 21:40