Click here to Skip to main content
14,107,154 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
i want to replace

document.write()

to

w()

can it be happen ? i want to do it between script tags, i dont want to make from from other programs like c, python. i m beginer i dont know anything.

What I have tried:

a = document.write()

b = a.toString();

c = b.split('',-1);

w= w().toString().split('',-1);

w = a;



means whenever i will write this

w("hello!");

then it must behave like this on browser



hello!


without typing

document.write("hello");



can it be happen ?
Posted
Updated 26-Jul-16 9:21am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Yeah it's possible, just create a wrapper function. Not sure why you'd want to this however, but...
<script>
  // define it
  function w(markup) {
	document.write(markup);
  }

  // call it
  w("<p>Hello World</p>");
</script>
   
v2
Comments
Jeremy Falcon 27-Jul-16 13:08pm
   
In this example, you're close but the "markup" parameter should be what you want to show rather than the ID. Also, for the id() function you'll need to return the value. And last but not least you need to make sure the DOM element (the paragraph in this example) exists before you can call it. Try something like this...

<script>
function id(element) {
return document.getElementById(element);
}

// this cannot be called yet because the p element does not exist
id("demo").innerHTML = "i will not show";
</script>

<p id="demo"></p>

<script>
// now the dom element exists
id("demo").innerHTML = "i will show";
</script>
prasad sawant 27-Jul-16 14:28pm
   
wow! it works great.. thanks..

all i have understood that

1. the return syntax must be written

2. element must be there on top of script tag

i will understand it more by time i think..



i have question..

can i make this..

.innerHTML as .in()
.length as .len
.toString() as .str()

and so on..

means when will i type

id("demo").in("hello");

it will show..

hello

instead of..

document.getElementById("demo").innerHTML="hello";
Jeremy Falcon 27-Jul-16 15:43pm
   
Yes you could do that, with one notable difference, .innerHTML and .length are properties. As such you'd have to either write a wrapper method for it too, or a new prototype that calls the real one underneath the hood, but on the same object. Since code is worth a thousand words...

<script>
function id(element) {
return document.getElementById(element);
}

Object.prototype.in = function(markup) {
this.innerHTML = markup;
}
</script>

<p id="demo"></p>

<script>
// now the dom element exists
id('demo').in('i will show');
</script>
prasad sawant 28-Jul-16 9:32am
   
i didnt get it well but i hope i will know it soon, what i got here is

new syntax-- Object.prototype and this

thank u very much
prasad sawant 29-Jul-16 7:58am
   
read my new post, hope u have answer of it.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

thank u very much its working!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 26 Jul 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100