Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
23 May, 2013, 23:45:31 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1] 2   Go Down
  Print  
Author Topic: html execution in javascript? -  (Read 1428 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« on: 05 August, 2011, 03:05:09 »
0

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14

<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: 05 August, 2011, 03:06:25 by Snake X » Logged

There's a Zombie for that™
calcdude84se
Needs Motivation
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 16:12:14
Date Registered: 21 April, 2010, 04:20:59
Posts: 2207


Total Post Ratings: +62

View Profile
« Reply #1 on: 05 August, 2011, 03:10:14 »
0

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: 05 August, 2011, 03:10:56 by calcdude84se » Logged

"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Bug me about PartesOS. I might just need reminding.
BlakPilar
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 20 February, 2013, 02:38:22
Date Registered: 16 July, 2011, 02:50:55
Posts: 735


Total Post Ratings: +43

View Profile
« Reply #2 on: 05 August, 2011, 03:14:35 »
0

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:

1
2
3
4
5
6
7
8
9
10
11
12
<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: 05 August, 2011, 03:17:37 by BlakPilar » Logged
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« Reply #3 on: 05 August, 2011, 03:18:57 »
0

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: 05 August, 2011, 03:21:02 by Snake X » Logged

There's a Zombie for that™
BlakPilar
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 20 February, 2013, 02:38:22
Date Registered: 16 July, 2011, 02:50:55
Posts: 735


Total Post Ratings: +43

View Profile
« Reply #4 on: 05 August, 2011, 03:27:37 »
0

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: 05 August, 2011, 03:29:27 by BlakPilar » Logged
calcdude84se
Needs Motivation
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 16:12:14
Date Registered: 21 April, 2010, 04:20:59
Posts: 2207


Total Post Ratings: +62

View Profile
« Reply #5 on: 05 August, 2011, 03:29:39 »
0

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:

1
2
3
4
5
<script type="text/javascript">
//<![CDATA[
alert("Code goes here")
//]]>
</script>
It also makes it XHTML-compatible.
Edit: ninja'd
« Last Edit: 05 August, 2011, 03:30:01 by calcdude84se » Logged

"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Bug me about PartesOS. I might just need reminding.
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« Reply #6 on: 05 August, 2011, 03:56:32 »
0

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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<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: 05 August, 2011, 04:29:51 by Snake X » Logged

There's a Zombie for that™
calcdude84se
Needs Motivation
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 16:12:14
Date Registered: 21 April, 2010, 04:20:59
Posts: 2207


Total Post Ratings: +62

View Profile
« Reply #7 on: 05 August, 2011, 04:38:52 »
0

You slightly misinterpreted what I said Tongue
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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: 05 August, 2011, 04:41:16 by calcdude84se » Logged

"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Bug me about PartesOS. I might just need reminding.
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« Reply #8 on: 05 August, 2011, 05:19:28 »
0

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: 05 August, 2011, 05:22:39 by Snake X » Logged

There's a Zombie for that™
AngelFish
This is my custom title
Administrator
LV12 Extreme Poster (Next: 5000)
*
Offline Offline

Gender: Male
Last Login: Today at 08:17:37
Date Registered: 15 August, 2010, 09:18:54
Posts: 3190


Total Post Ratings: +220

View Profile
« Reply #9 on: 05 August, 2011, 05:43:18 »
0

What are you trying to do? If it's generating pages, that's probably best done in PHP or similar.
Logged

∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ
cooliojazz
Support Staff
LV7 Elite (Next: 700)
*
Offline Offline

Gender: Male
Last Login: Today at 21:23:52
Date Registered: 23 May, 2009, 19:28:11
Location: Colorado, USA
Posts: 614


Total Post Ratings: +52

View Profile WWW
« Reply #10 on: 05 August, 2011, 05:56:16 »
0

You could use php...

1
2
3
4
5
6
7
8
9
10
<html>
<body>
<?php if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "chrome") > -1) { ?>
<!-- Chrome html here -->
<?php } else { ?>
<!-- Display error! -->
<?php ?>
</body>
</html
Logged

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)
BlakPilar
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: 20 February, 2013, 02:38:22
Date Registered: 16 July, 2011, 02:50:55
Posts: 735


Total Post Ratings: +43

View Profile
« Reply #11 on: 05 August, 2011, 05:57:32 »
0

Guys, don't you only need to use the CDATA tag when the page is in XHTML?
Logged
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« Reply #12 on: 05 August, 2011, 14:06:04 »
0

You could use php...

1
2
3
4
5
6
7
8
9
10
<html>
<body>
<?php if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "chrome") > -1) { ?>
<!-- Chrome html here -->
<?php } else { ?>
<!-- Display error! -->
<?php ?>
</body>
</html

I have used your suggestion and made this:

1
2
3
4
5
6
7

<?php if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']), "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: 05 August, 2011, 14:10:52 by Snake X » Logged

There's a Zombie for that™
calcdude84se
Needs Motivation
Members
LV11 Super Veteran (Next: 3000)
***********
Offline Offline

Gender: Male
Last Login: 14 May, 2013, 16:12:14
Date Registered: 21 April, 2010, 04:20:59
Posts: 2207


Total Post Ratings: +62

View Profile
« Reply #13 on: 05 August, 2011, 14:20:10 »
0

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: 05 August, 2011, 14:20:30 by calcdude84se » Logged

"People think computers will keep them from making mistakes. They're wrong. With computers you make mistakes faster."
-Adam Osborne
Bug me about PartesOS. I might just need reminding.
Snake X
LV8 Addict (Next: 1000)
********
Offline Offline

Gender: Male
Last Login: Today at 14:11:47
Date Registered: 18 March, 2010, 02:48:47
Posts: 806


Topic starter
Total Post Ratings: +25

View Profile
« Reply #14 on: 05 August, 2011, 14:45:43 »
0

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: 05 August, 2011, 14:46:02 by Snake X » Logged

There's a Zombie for that™
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.451 seconds with 32 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.