Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


98 posts

Master Geek


Topic # 29358 4-Jan-2009 20:14
Send private message

Hi guys, I'm not very familiar with JavaScript. Is there any way of doing the following with Javascript?

var x = document;
var y = .write;
var c = x+y;
c("Hello World!");



Create new topic
Phil Gale
1107 posts

Uber Geek
+1 received by user: 44

Trusted
Red Jungle
Subscriber

  Reply # 187362 4-Jan-2009 20:23
Send private message

You want to combine two variables and then be able to call that variable as if it were a function? or you want to be able to write some output somewhere?

Perhaps if you can tell me more about what you're trying to achieve I might be able to suggest a better/more useful solution?




Red Jungle: we make fantastic software

RSS  Twitter  Facebook  Skype

8027 posts

Uber Geek
+1 received by user: 387

Trusted
Subscriber

  Reply # 187365 4-Jan-2009 20:36
Send private message

Yeah what RedJungle said, what are you trying to do?  It's pretty rare to write directly to the document thesedays, normally if you want to insert content into the page via javascript you'd have a div tag and use javascript to find that element in the document object model via id or class then insert content into the div.



98 posts

Master Geek


  Reply # 187387 4-Jan-2009 21:38
Send private message

well that was just an example. I am wondering if I can put two variables together to perform a command.

I have a function which creates a layer and brings up a bunch of colours to choose from. Depending on the arguments I pass to that function I want it to change what action it performs. i.e (with another simplified example)

I have a button called [change text colour] and onclick it calls my color function and sends 3 arguments "window.mytext", ".style.color" and "#FFFFFF".  I also have a button called change [background colour] and it also passes 3 arguments "window.divbgcolour", ".style.backgroundColor" and "#000000".

so I want my function to do this:

function setcolor(inputobj, inputstyle, colour) {
  inputobj+inputstyle = colour;
}

so far I've had no luck with making that work. I can however get it to work this way:

function setcolor(inputobj, inputstyle, colour) {
  if (inputstyle == '.style.backgroundColor') {
    inputobj.style.backgroundColor = colour;
  }
  if (inputstyle == '.style.color') {
    inputobj.style.color = colour;
  }
}

Phil Gale
1107 posts

Uber Geek
+1 received by user: 44

Trusted
Red Jungle
Subscriber

  Reply # 187397 4-Jan-2009 22:07
Send private message

I get what you're after. Sadly, I think your current approach is as close as you'll get really.

By the looks of things, the first parameter 'inputobj' is a reference to the actual DOM object you want so you're able to access it fine. But for 'inputstyle', unless you pass a reference to the actual object (which would defeat the objective I imagine), its just going to be interpretted as a string.

I would probably take a similar approach using if/switch statements to handle each case.




Red Jungle: we make fantastic software

RSS  Twitter  Facebook  Skype

6328 posts

Uber Geek
+1 received by user: 391

Moderator
Trusted
Lifetime subscriber

  Reply # 187410 5-Jan-2009 00:09
Send private message

The eval() function evaluates a string and executes it as if it was script code.

Concatenate what you are trying to do together, then pass it to eval.

1243 posts

Uber Geek
+1 received by user: 156


  Reply # 187418 5-Jan-2009 01:22
Send private message


function setcolor(inputobj, inputstyle, colour) {
  inputobj+inputstyle = colour;
}


Use the associative array syntac for object referencing.

function setcolor(inputobj, property, colour) {
  inputobj.style[property] = colour;
}

setcolor(document.getElementById('something'), 'backgroundColor', '#EFEFEf');




---
James Sleeman
I sell lots of stuff for electronic enthusiasts...


Phil Gale
1107 posts

Uber Geek
+1 received by user: 44

Trusted
Red Jungle
Subscriber

Reply # 187439 5-Jan-2009 09:21
Send private message

nate: The eval() function


Ha! Forgot about that one. Nate's onto it.




Red Jungle: we make fantastic software

RSS  Twitter  Facebook  Skype



98 posts

Master Geek


  Reply # 187595 5-Jan-2009 17:15
Send private message

thanks for all the help.I'll give the eval() function a go tonight.

8027 posts

Uber Geek
+1 received by user: 387

Trusted
Subscriber

  Reply # 187610 5-Jan-2009 18:13
Send private message

sleemanj:

Use the associative array syntac for object referencing.

function setcolor(inputobj, property, colour) {
  inputobj.style[property] = colour;
}

setcolor(document.getElementById('something'), 'backgroundColor', '#EFEFEf');


This way is recommend way over using Eval in this situation imo

Refer to:  http://24ways.org/2005/dont-be-eval

Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.