...

View Full Version : document.write PLEASE HELP



ufgcorp
05-23-2012, 08:16 AM
I'm trying to get this to work in Magento CMS, so need it to write the returned value (which is a phone number) into a page, but at present it is writing the phone number but on a blank page (so getting rid of everything else) I think because of the document.write
Is there anyone who can tell me how to do this so it writes the phone number in the space I put this code???
Much appreciated!

<script>jQuery(document).ready(function(){document.write(jQuery(".fulfiller-contact-telephone").text())});</script>

_Aerospace_Eng_
05-23-2012, 08:21 AM
You will need to have span or a div with an ID then your code would be

<script type="text/javascript">jQuery(document).ready(function(){
var phone = jQuery(".fulfiller-contact-telephone").text();
jQuery('#phonehold').append(phone);
});</script>
Then where you want the number to display add

<span id="phonehold"></span>
Another option would be to copy the html that holds the phone number to where you want it.

Philip M
05-23-2012, 08:30 AM
document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. However, you can use it to write to child pages.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

ufgcorp
05-23-2012, 08:48 AM
Thankyou, exactly what I needed. Very much appreciated!

felgall
05-23-2012, 10:04 AM
The only time you need to use document.write to create child pages instead of using a server side script is if you are running the script in the browser itself and not in any specific web page. One such example would be a script that generates a copy of the current page source with nice links to all of the files it uses where the page to display that source is generated using a document.write statement in the userscript attached to the browser.

There is never any need for document.write in scripts attached to a web page directly.

Since attaching scripts to the browser rather than to a web page is a more advanced topic, document.write has no place in beginners or intermediate JavaScript courses - it belongs in the advanced course as that's the only place where it is needed.

Philip M
05-23-2012, 01:40 PM
As felgall has said, your course was really "A History Of JavaScript". It may have been fine in 1999. Not in 2012. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum