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


# 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
1108 posts

Uber Geek

Trusted
Red Jungle
Subscriber

  # 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

8034 posts

Uber Geek

Trusted

  # 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


  # 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
1108 posts

Uber Geek

Trusted
Red Jungle
Subscriber

  # 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

6358 posts

Uber Geek

Moderator
Trusted
Lifetime subscriber

  # 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.

1287 posts

Uber Geek


  # 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
1108 posts

Uber Geek

Trusted
Red Jungle
Subscriber

# 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


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

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

8034 posts

Uber Geek

Trusted

  # 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 and LinkedIn »



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:





News »

Arlo unveils its first video doorbell
Posted 21-Oct-2019 08:27


New Zealand students shortlisted for James Dyson Award
Posted 21-Oct-2019 08:18


Norton LifeLock Launches Norton 360
Posted 21-Oct-2019 08:11


Microsoft New Zealand Partner Awards results
Posted 18-Oct-2019 10:18


Logitech introduces new Made for Google keyboard and mouse devices
Posted 16-Oct-2019 13:36


MATTR launches to accelerate decentralised identity
Posted 16-Oct-2019 10:28


Vodafone X-Squad powers up for customers
Posted 16-Oct-2019 08:15


D Link ANZ launches EXO Smart Mesh Wi Fi Routers with McAfee protection
Posted 15-Oct-2019 11:31


Major Japanese retailer partners with smart New Zealand technology IMAGR
Posted 14-Oct-2019 10:29


Ola pioneers one-time passcode feature to fight rideshare fraud
Posted 14-Oct-2019 10:24


Spark Sport new home of NZC matches from 2020
Posted 10-Oct-2019 09:59


Meet Nola, Noel Leeming's new digital employee
Posted 4-Oct-2019 08:07


Registrations for Sprout Accelerator open for 2020 season
Posted 4-Oct-2019 08:02


Teletrac Navman welcomes AI tech leader Jens Meggers as new President
Posted 4-Oct-2019 07:41


Vodafone makes voice of 4G (VoLTE) official
Posted 4-Oct-2019 07:36



Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.