Welcome Guest [Log In] [Register]
Search Members Calendar | Rules ZB Code Index IF Code Index
ZBCode
  • Navigation
  • ZBCode
  • Coding Support
  • Code University
  • Groups in ZB
Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ;)

Interested in joining? Click here.


If you are already a member of ZBCode, feel free to login right here:

Username:   Password:
Locked Topic
  • Pages:
  • 1
  • 2
  • 3
Groups in ZB
Tweet Topic Started: Jun 10 2009, 06:33 PM (593 Views)
Dorith Jun 10 2009, 10:41 PM Post #11
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
Hmm, seems a lot longer and more complicated than I imagined it would be.
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
Reid Jun 10 2009, 11:50 PM Post #12
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Haha yeah. Things with JavaScript tend to be like that.
The Resource Board
Offline Profile Goto Top
 
Dorith Jun 11 2009, 02:39 AM Post #13
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
Well back to basic document.write :'( cause I didn't really understand that code

Edit:
Instead of
Code:
 
document.write('blah');

If I were to switch it to something like
Code:
 
document.getElementByID("a.member").append(" Sponsor")

Would that work so that the member that can see "said" forum, could have their name added "Sponsor" at the end like there?

Also would this work every time, or only when that user actually sees that forum?

Did I even use the append feature correctly?
Edited by Dorith, Jun 11 2009, 02:51 AM.
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
Reid Jun 11 2009, 01:09 PM Post #14
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Dorith
Jun 11 2009, 02:39 AM
Well back to basic document.write :'( cause I didn't really understand that code

Edit:
Instead of
Code:
 
document.write('blah');

If I were to switch it to something like
Code:
 
document.getElementByID("a.member").append(" Sponsor")

Would that work so that the member that can see "said" forum, could have their name added "Sponsor" at the end like there?

Also would this work every time, or only when that user actually sees that forum?

Did I even use the append feature correctly?
Haha no, that isn't right. Although, if you were using jQuery, it would work. Kind of.

Firstly, that isn't an ID you're getting. IDs exist only once on a particular page, and a.member is a jQuery selector (or a CSS selector, either one of the two.) .member signifies a class of 'member.' For some reason, the creators of JS decided to be difficult and not include a getElementsByClass or anything similar. So you're stuck looping through every <a> tag on the page manually, like so...
Code:
 
// grab all of the links on the page
var a = document.getElementsByTagName('a');
// get the length so we can loop
var b = a.length;
/* while loops allow us to do something over and over
while a specific condition is true. in this case,
we are saying while b-- so that the loop runs
as long as b is not 0.
since length is ALWAYS positive, and arrays have indexes,
document.getElementsByTagName() gives us an array
so, yeah. */
while (b--){
/* the length alone will never work
that is, you can't use the length to get the last element
that's because if you have y = [ 1,2,3,4 ]
y.length is 4
but arrays start at 0, so the last part is y[3]
so y[4] doesn't exist.
so you might think we'd get an error,
but b-- is a decrement; that is, it gives 1 less
so if b = 5, and you do b--, you get 4. easy, right?
while loops don't actually do this by default,
it has to check the condition, so it automatically
makes it 1 less.
so when it looks at it, it goes "is b-- true?"
so it does b-- the first time
this allows us to loop through elements quickly and efficiently */
if (a[b].className == 'member') {
// since a is an array and b is our length, it starts at the last element
// so a[b] is a link (<a> tag) always thanks to this
// so if we had 5 <a> tags on the page, a[b] would be a[4] thanks
// to the b-- we have in the condition
a[b].innerHTML += 'some stuff';
/* += is another operator
it's pretty simple, but it saves on coding time
basically, if you had y = 1 and z = 2,
and you do y += z, you would get 3.
this is because y += z is the same thing as
y = y + z
these operators exist with all 4 basic math functions
you could do y -=z , y *= z , y /= z, etc
innerHTML is what is inside of an element
it doesn't return ONLY text, it returns all of the HTML in it
this means we can add more HTML to an element if we want, or remove some */
}
}
I wrote out a little description in comments for you. Again, I highly recommend getting Notepad++ so you can see the difference between the comments and the actual javascript going on there.
The Resource Board
Offline Profile Goto Top
 
Dorith Jun 11 2009, 03:30 PM Post #15
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
So let's supposed this code:
Code:
 
<a href="www.com">Website</a>

Would .innerHTML be website or www.com?

And how would I reference the difference between those to? I'm thinkin of creating this one code, but I don't want you to know what it is yet, cause you'll probably end up writing it up. And I REALLY wanna figure this out :P
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
Reid Jun 11 2009, 06:46 PM Post #16
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Dorith
Jun 11 2009, 03:30 PM
So let's supposed this code:
Code:
 
<a href="www.com">Website</a>

Would .innerHTML be website or www.com?

And how would I reference the difference between those to? I'm thinkin of creating this one code, but I don't want you to know what it is yet, cause you'll probably end up writing it up. And I REALLY wanna figure this out :P
Well you've got to have some way to find that element, but the innerHTML will be "website" and if you want to get the href, you just do
Code:
 
// some_random_variable is that <a> tag
some_random_variable.href //will return "www.com"
(without the quotes)

If you have Firebug (and if you don't, I suggest you get it :P ) then you can type this into the console to see what I mean.
Code:
 
//don't worry about this part
var c = document.createElement('a');
c.href = "www.com";
c.innerHTML = "Website";
// you can start reading now :P
alert(c.href);
alert(c.innerHTML);
It'll give you two alerts. I created your imaginary element in the first 3 lines, as well.

edit: typo
Edited by Reid, Jun 11 2009, 06:46 PM.
The Resource Board
Offline Profile Goto Top
 
Reid Jun 11 2009, 06:52 PM Post #17
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
GahhhhhH! Six hundred and sixty six posts! /destroy
The Resource Board
Offline Profile Goto Top
 
Dorith Jun 11 2009, 07:06 PM Post #18
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
So I made these:
Code:
 
<script type="text/javascript">
var a = document.getElementsByTagName('a');
var b = a.length;
while (b--){
if(a[b].innerHTML == 'Grey'){
a[b].innerHTML += ' Sponsor';
}
}
</script>

Code:
 

<script type="text/javascript">
var sponsor = "Grey Sponsor"
var name = document.getElementsByTagName('a');
var findname = name.length
var changename = findname.replace('Grey', sponsor);
</script>


Not sure how to execute that last one. Tell me what I did wrong (cause I'm sure something is wrong), and stuff...
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
Reid Jun 11 2009, 08:05 PM Post #19
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Dorith
Jun 11 2009, 07:06 PM
Code:
 

<script type="text/javascript">
var sponsor = "Grey Sponsor"
var name = document.getElementsByTagName('a');
var findname = name.length
var changename = findname.replace('Grey', sponsor);
</script>
Woah. So... You want to find the name 'Grey' and replace it with 'Grey Sponsor'?

Let's take this one step at a time then. Your way doesn't work because... you have to loop through the links somehow. Like.. this
Code:
 
var sponsor = "Grey Sponsor";
var name = document.getElementsByTagName('a');
var findname = name.length;
while (findname--) {
name[findname] = name[findname].replace('Grey',sponsor);
}
'var' stands for variable. So
Code:
 
var x = 1;
is an example. You don't ever have to use the var keyword either - although you are supposed to when you first define a variable.
The Resource Board
Offline Profile Goto Top
 
Dorith Jun 11 2009, 08:59 PM Post #20
Member Avatar
Has just entered the Matrix

Posts:
2,069
Group:
Former Staff
Member
#1,854
Joined:
Dec 23, 2008
:O OH SNAP! I almost got it! I actually sorta set that up, except in like 2 different codes. :D Hmm, now I'm gonna try and code it to be more efficient. And maybe code it another way by adding it to the beginning or end of that found person.

I'm sorta getting this now though I don't get this part completely:
Code:
 
while (findname--) {
name[findname] = name[findname].replace('Grey',sponsor);
}

You said something about it before, but do it noob talk. Like why name[findname]? And what are those "--" for?

Also what is "!-=1"? Cause I see it a lot in your guys' codes.
Posted Image
Posted Image
(Made emoticon using Codes Rock's Smiley Generator)
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Code University · Next Topic »
Locked Topic
  • Pages:
  • 1
  • 2
  • 3

Track Topic · E-mail Topic Time: 7:46 PM Jul 10
Hosted for free by ZetaBoards · Privacy Policy