PDA

View Full Version : Noob javascript quetsion



traveller59
09-20-2012, 09:54 AM
I just started learn javascript.
How do I make it write Hello World!?
thanks traveller59

keyboard
09-20-2012, 09:57 AM
There are two main ways of doing this.

One -



<!DOCTYPE html>
<html>
<head>
<script type="text/ecmascript">
/*
"Hello World" script v1.0
*/
function collect_text() {
//The text that you want to write
return "Hello World!";
}
function collect_id() {
//The id for the p tag
return "p1";
}
function choose_append_type() {
/*
If 1, script will use appendChild();
If 2, script will use innerHTML
*/
return 2;
}
function choose_default_warning_reporting() {
/*
Must be either true or false
*/
return "false";
}
function choose_warning_reporting() {
/*
Must be either true or false
*/
return "false";
}

/*
// --------------------------------
// DO NOT EDIT BELOW HERE!!!!!!!!!!
// --------------------------------
*/
var pTag_Text;
onload = (function() {
var warning_reporting = choose_warning_reporting();
var default_warning_reporting = choose_default_warning_reporting();
if(typeof default_warning_reporting == "undefined") {
var default_warning_reporting = "false";
}
if((typeof warning_reporting) == ("undefined")) {
warning_reporting = default_warning_reporting;
} else {
if(((warning_reporting) != ("true")) && ((warning_reporting) != ("false"))) {
warning_reporting = default_warning_reporting;
}
}
pTag = document.createElement("p");
var id_check_numeric = collect_id();
var er1 = "false";
var t = 9;
var u = 0;
while(("qwertyuiopasdfghjklzxcvbnm") === ("qwertyuiopasdfghjklzxcvbnm")) {
if((u) > (t)) {
break;
} else {
if((id_check_numeric[0]) == (u)) {
er1 = "true";
}
u = u + parseInt("1");
}
}
if(((er1) == ("true")) && ((warning_reporting) == ("true"))) {
alert("Warning: First character of id should not be an interger");
}
pTag.id = collect_id();
var x = 0;
var y;
run_function(function() {
y = get_string_length(collect_text());
});
while(("qwertyuiopasdfghjklzxcvbnm") === ("qwertyuiopasdfghjklzxcvbnm")) {
if((x) >= (y)) {
break;
} else {
var elem1 = collect_text().charAt(x);
var append_func = (function() {
pTag.appendChild(pTag_Text);
});
if((typeof append_func) != ("undefined")) {
pTag_Text = document.createTextNode(elem1);
run_function(function() {
append_func();
});
}
x = x + parseInt("1");
}
}
run_function(function() {
append_to_document(pTag, collect_id(), choose_append_type());
});
run_function(function() {
fail_safe(collect_text(), collect_id());
});
});
function run_function(func1) {
if((typeof func1) != ("undefined")) {
func1();
}
}
function get_string_length(string1) {
var counter = 0;
while(("qwertyuiopasdfghjklzxcvbnm") === ("qwertyuiopasdfghjklzxcvbnm")) {
if((counter) >= (string1.length)) {
return counter;
break;
} else {
counter = counter + parseInt("1");
}
}
}
function append_to_document(string1, id1, append_method) {
if((typeof append_method) == ("undefined")) {
var append_method = 1;
alert("bob");
}
if((append_method) == (1)) {
document.body.appendChild(pTag);
} else if((append_method) == (2)) {
var document_body_contents = document.body.innerHTML
document.body.innerHTML = document_body_contents + '<p id="' + id1 + '">' + string1.innerHTML + '</p>';
} else {
document.body.appendChild(pTag);
}
}
function fail_safe(string1, id1) {
var document_body_contents = document.body.innerHTML;
var search_document_body_contents = document_body_contents.search(string1);
if((search_document_body_contents) <= (0)) {
//Somehow, if all of the above code failed...
document.write('<p id="' + id1 + '">' + string1 + '</p>');
}
}
</script>
</head>
<body>
</body>
</html>


And two -


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
document.write("Hello World");
</script>
</head>
<body>
</body>
</html>


Either should work for you, but IMHO the first one is simpaler...
This is a fairly basic question, so I'd suggest you look at a javascript tutorial to learn the basics.

bernie1227
09-20-2012, 10:22 AM
well keyboard, you could shorten that first function a lot by going:


document.write('<p id="words">');
var message = 'hello world';
var thing = document.getElementById('words');
for(i = 0; i < strlen(message); i++){
thing.append(message[i]);
}
document.write('</p>');

I assume that would work anyway :p

traveller59
09-20-2012, 11:03 AM
thanks for the quick replies
i tried your code keyboard but it didnt work
i put it into the editor and saved it then ran it and the code just showed on the page
heres the code for the page



bernie how do i use your code

bernie1227
09-20-2012, 11:04 AM
what code?

for my code, simply put it with in javascript <script> tags on your webpage (in the head)

traveller59
09-20-2012, 11:05 AM
it wonr let me write the code

bernie1227
09-20-2012, 11:05 AM
this (http://www.scriptingmaster.com/javascript/running-javascript.asp) gives a quick tutorial on using javascript.

traveller59
09-20-2012, 11:05 AM
i just copied and pasted it into the document

traveller59
09-20-2012, 11:07 AM
how do i attach a page/

bernie1227
09-20-2012, 11:10 AM
to link a page, use url tags, eg,
[url="your-site.com"]

traveller59
09-20-2012, 11:20 AM
i put te page on a site called box
https://www.box.com/s/alneidxr1xh9ytkgqj50