Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 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

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

  // call it
  w("<p>Hello World</p>");
</script>
 
Share this answer
 
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.
thank u very much its working!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900