Author Topic: html execution in javascript?  (Read 9024 times)

0 Members and 1 Guest are viewing this topic.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
html execution in javascript?
« 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>
« Last Edit: August 04, 2011, 09:06:25 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: html execution in javascript?
« Reply #1 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.
« Last Edit: August 04, 2011, 09:10:56 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: html execution in javascript?
« Reply #2 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>
« Last Edit: August 04, 2011, 09:17:37 pm by BlakPilar »

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: html execution in javascript?
« Reply #3 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.
« Last Edit: August 04, 2011, 09:21:02 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: html execution in javascript?
« Reply #4 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 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. (Unless someone wants to explain >.>)
« Last Edit: August 04, 2011, 09:29:27 pm by BlakPilar »

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: html execution in javascript?
« Reply #5 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
« Last Edit: August 04, 2011, 09:30:01 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: html execution in javascript?
« Reply #6 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
« Last Edit: August 04, 2011, 10:29:51 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: html execution in javascript?
« Reply #7 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.
« Last Edit: August 04, 2011, 10:41:16 pm by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: html execution in javascript?
« Reply #8 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!'
« Last Edit: August 04, 2011, 11:22:39 pm by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: html execution in javascript?
« Reply #9 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.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: html execution in javascript?
« Reply #10 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
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline BlakPilar

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 734
  • Rating: +44/-1
    • View Profile
Re: html execution in javascript?
« Reply #11 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?

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: html execution in javascript?
« Reply #12 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!
« Last Edit: August 05, 2011, 08:10:52 am by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.

Offline calcdude84se

  • Needs Motivation
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2272
  • Rating: +78/-13
  • Wondering where their free time went...
    • View Profile
Re: html execution in javascript?
« Reply #13 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.)
« Last Edit: August 05, 2011, 08:20:30 am by calcdude84se »
"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Spoiler For "PartesOS links":
I'll put it online when it does something.

Offline Snake X

  • Ancient Veteran
  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 810
  • Rating: +33/-8
    • View Profile
Re: html execution in javascript?
« Reply #14 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
« Last Edit: August 05, 2011, 08:46:02 am by Snake X »
Loved this place, still the best producers of power metal, and sparked my dreams of coding.