Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Snake X on August 04, 2011, 09:05:09 pm

Title: html execution in javascript?
Post by: Snake X on August 04, 2011, 09:05:09 pm
So, I built a nice piece of HTML on a page somewhere. However, it only shows up *properly* in chrome (dont ask what it is, just go along with the story). So I wanted to do something like this:

Code: [Select]

<script language="javascript" type="text/javascript">
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome === true)
  {
    <p>this is HTML</p>
    <p> all fancy shmancy html code goes here.</p>
  }
else
  {
     document.write("get chrome!");
  }
</script>
Title: Re: html execution in javascript?
Post by: calcdude84se on August 04, 2011, 09:10:14 pm
You shouldn't be putting HTML directly in JS code. That shouldn't even work. If you want to modify the page using Javascript, you have to go through the DOM, or use document.write like you did in the else clause. What you write should also be proper HTML.
So, the two interesting lines should be like this:
document.write("<p>This is HTML</p>")
document.write("<p>Get Chrome!</p>")
The only reason that might work is because the JS code is being parsed like HTML. There are a couple ways to prevent that. Use CDATA, or make the whole thing a comment. Either should work. Note that you should do this for any JS code, just in case.
Title: Re: html execution in javascript?
Post by: BlakPilar on August 04, 2011, 09:14:35 pm
Well, calcdude beat me over the fence (congrats if you get the reference). Like he said, writing HTML directly in JS is a no-no. You need to put it inside of quotes in a document.write().

EDIT: Doing a quick test in DW, this works:
Code: [Select]
<script language="javascript" type="text/javascript">
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome === true)
  {
     document.write("<p>this is HTML</p><p> all fancy shmancy html code goes here.</p>");
  }
else
  {
     document.write("get chrome!");
  }
</script>
Title: Re: html execution in javascript?
Post by: Snake X on August 04, 2011, 09:18:57 pm
err, what is CDATA? I'm (VERY) new to javascript to be honest. I wanted to learn enough just to make this work. Also what is DOM?

edit: I put my html in double quotes and it showed up blank. My html includes tables, swf embedding, font color, size changes, and ascii extended characters.
Title: Re: html execution in javascript?
Post by: BlakPilar on August 04, 2011, 09:27:37 pm
err, what is CDATA? I'm (VERY) new to javascript to be honest. I wanted to learn enough just to make this work. Also what is DOM?
CDATA stands for character data, but it allows non-XHTML pages to have actual JavaScript between the script tags, like you have. See here (http://javascript.about.com/library/blxhtml.htm) for more detail. (What I said might not make sense, so you might just want to look at the link.)

DOM stands for Document Object Model, for info look here (http://www.quirksmode.org/dom/intro.html). (Unless someone wants to explain >.>)
Title: Re: html execution in javascript?
Post by: calcdude84se on August 04, 2011, 09:29:39 pm
DOM stands for the Document Object Model. It describes an object-oriented interface to an HTML (or any XML) file.
As for CDATA, it is just a special section which is not parsed. The best way to do it is like this:
Code: [Select]
<script type="text/javascript">
//<![CDATA[
alert("Code goes here")
//]]>
</script>
It also makes it XHTML-compatible.
Edit: ninja'd
Title: Re: html execution in javascript?
Post by: Snake X on August 04, 2011, 09:56:32 pm
EDIT: it works.. kinda. However it will display in firefox when it should not have because of the if-then condition.

edit: here is my code:

Code: [Select]
<script language="javascript" type="text/javascript">
document.title=("Cell's Awesome Profile! :)")
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome === true)
  {

<script type="text/javascript">
//<![CDATA[
alert("
<p>i has chrome yay</p>
")
//]]>

  }
else
  {
     document.write("get chrome!");
  }
</script>

edit 2: the above code now does not work like at all. It is blank nomatter what browser u look at
Title: Re: html execution in javascript?
Post by: calcdude84se on August 04, 2011, 10:38:52 pm
You slightly misinterpreted what I said :P
You don't use another <script> tag. The whole script should be in a CDATA section.
And the "alert(..." line in the example I gave stood for any JS code, given that it was a stand-in for the entire JS script.
Edit: thus, the fixed version of your code is
Code: [Select]
<script language="javascript" type="text/javascript"> //<![CDATA[
document.title=("Cell's Awesome Profile! :)")
var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (is_chrome === true)
  {

    document.print("<p>i has chrome yay</p>")

  }
else
  {
     document.write("<p>get chrome!</p>");
  }
//]]>
</script>
Edit 2: also, you didn't put <p></p> around the other text. Fixed.
Title: Re: html execution in javascript?
Post by: Snake X on August 04, 2011, 11:19:28 pm
I copied and pasted exactly what you had and it didnt show anything in chrome. However it displayed 'get chrome!' in firefox.

edit: is there any other way to do this exact same thing in other browser languages besides js? I mean like if my browser is chrome, execute all html, if not display 'yu needs chrome!'
Title: Re: html execution in javascript?
Post by: AngelFish on August 04, 2011, 11:43:18 pm
What are you trying to do? If it's generating pages, that's probably best done in PHP or similar.
Title: Re: html execution in javascript?
Post by: cooliojazz on August 04, 2011, 11:56:16 pm
You could use php...
Code: [Select]
<html>
<body>
<?php if (strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), "chrome") > -1) { ?>
<!-- Chrome html here -->
<?php } else { ?>
<!-- Display error! -->
<?php ?>
</body>
</html
Title: Re: html execution in javascript?
Post by: BlakPilar on August 04, 2011, 11:57:32 pm
Guys, don't you only need to use the CDATA tag when the page is in XHTML?
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 08:06:04 am
You could use php...
Code: [Select]
<html>
<body>
<?php if (strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), "chrome") > -1) { ?>
<!-- Chrome html here -->
<?php } else { ?>
<!-- Display error! -->
<?php ?>
</body>
</html

I have used your suggestion and made this:
Code: [Select]

<?php if (strpos(strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]), "chrome") > -1) { ?>
<!-- <p><font face="arial" size="24"><b>IM USING CHROME!!! </b></font></p> -->
<?php } else { ?>
<!-- <p>get chrome!</p> -->
<?php ?>

however this is displayed on both chrome and firefox: -1) { ?>

edit: if i take out the <!-- and --> on both, it displays all 3:

-1) { ?>
IM GETTING CHROME
get chrome!
Title: Re: html execution in javascript?
Post by: calcdude84se on August 05, 2011, 08:20:10 am
PHP is server-side. It needs to be run on the server. I don't know where you're testing this from, but, if you're testing it on your computer, just viewing the page in a web browser won't work. You'll have to set up a (small, temporary) webserver of your own on your computer to test. Now, if you're uploading this every time and testing it on your website, then make sure your host has PHP enabled. (Most all do.)
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 08:45:43 am
im testing it on a webs page. Also where im putting this php up isnt gonna be on a server, its going to be on a chatango profile.

edit: http://greyshadowencodes.webs.com/detection.htm
Title: Re: html execution in javascript?
Post by: cooliojazz on August 05, 2011, 09:19:49 am
Try changing that to detection.php ;)
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 11:44:29 am
well I can't change it to .php where the final version of this code will be used at. Is there a way I can do google chrome detection the same way I did in css? that's probably my only hope now
Title: Re: html execution in javascript?
Post by: cooliojazz on August 05, 2011, 11:56:58 am
Sorry, didn't exactly read the last post :P Try this...
Code: [Select]
<div id="chrome">You have chrome!</div>
<div id="nochrome">You don't have chrome =\</div>
<script type="text/javascript">
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        document.removeChild(document.getElementById("nochrome"));
    } else {
        document.removeChild(document.getElementById("chrome"));
    }
</script>
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 12:28:49 pm
it outputs this in chrome and firefox:

You have chrome!
You don't have chrome =\

:(
Title: Re: html execution in javascript?
Post by: Deep Toaster on August 05, 2011, 01:31:56 pm
Guys, don't you only need to use the CDATA tag when the page is in XHTML?
Yeah, that. The only point of it is to protect the well-structured XML.
Title: Re: html execution in javascript?
Post by: calcdude84se on August 05, 2011, 01:41:14 pm
You should be using XHTML anyway ;D
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 01:47:50 pm
well not all of us know xhtml.. lol. I only know html and how to construct html 5
Title: Re: html execution in javascript?
Post by: calcdude84se on August 05, 2011, 01:48:54 pm
XHTML is just HTML that actually obeys XML standards. That's all.
So, instead of <br>, use <br/> (or <br /> to make some HTML parsers happy).
Title: Re: html execution in javascript?
Post by: BlakPilar on August 05, 2011, 01:49:51 pm
it outputs this in chrome and firefox:

You have chrome!
You don't have chrome =\

:(
Here's some browser information to help you refine your check :)
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 01:52:48 pm
so then in that case, the code has to be specific enough to identify chrome from firefox.. Hmm. This im not sure how to do. Man i might be trying to do the impossible really :S
Title: Re: html execution in javascript?
Post by: Deep Toaster on August 05, 2011, 03:23:41 pm
The only thing you need to pay attention to is the user agent header. Chrome is the only one that has the word "Chrome" in it.
Title: Re: html execution in javascript?
Post by: cooliojazz on August 05, 2011, 03:43:15 pm
See, that's what i get for just writing code without actually trying it =P Try this:
Code: [Select]
<div id="chrome">You have chrome!</div>
<div id="nochrome">You don't have chrome =\</div>
<script type="text/javascript">
    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        document.body.removeChild(document.getElementById("nochrome"));
    } else {
        document.body.removeChild(document.getElementById("chrome"));
    }
</script>
Title: Re: html execution in javascript?
Post by: Snake X on August 05, 2011, 05:20:54 pm
OOOOO
MMMMMM
GGGGGGGGG

THANK YOU SOO MUCH!!! IT WORKS :D
Title: Re: html execution in javascript?
Post by: cooliojazz on August 05, 2011, 09:25:57 pm
np :) Glad it worked for what you needed :)