Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
getElementById from external document
Old 06-26-2008, 11:28 PM getElementById from external document
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Hey gang, me again.

I was wondering if there is an equivelant of document.getElementById expect for an external document.

I have a link that says "Login" at the top of my page that I have change to "Logout" when you are logged in. The problem is that because I load the top menu bar before I check for the users status I need to use getElementById to change innerHTML and HREF. The problem comes from the fact that I made this site in php and have all the pages load off of the main page (probably not the best idea in retrospect but oh well, live and learn).

Thanks.
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-26-2008, 11:48 PM Re: getElementById from external document
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,023
Name: Forrest Croce
Location: Seattle, WA
Trades: 0
I don't think I understand the problem, but would deferring the script until after the page has rendered fix this?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 06-27-2008, 12:02 AM Re: getElementById from external document
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
ok I will break it down.

<html>
<body>
<a href="site1">site1</a>
<script>random script</script>
</body>
</html

The anchor is already displayed before the script runs so I need to be able to change the anchor info. In this example I would just use document.getElementById and I could change all the info.

In real life I have the script in a different place then the html. Short of recoding my page (which needs to be done eventually anyways), I am looking for a quick fix that can use getElementById but without referencing the current document that the script is in but the document that the html is in instead. Like "index.php".getElementById or something to that effect.

Hopefully that made more sense.
__________________
DVD Movie Release Database:
Please login or register to view this content. Registration is FREE
Truly is offline
Reply With Quote
View Public Profile
 
Old 06-27-2008, 11:45 AM Re: getElementById from external document
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Not totally sure what you're after, but you may want to start by reading about the "same origin policy" in Javascript. http://www.mozilla.org/projects/secu...tml#sameorigin

You can't have inline Javascript, and expect it to change or access another page. This is why it is best to separate your most important scripts, so that it can be linked to from various pages. In fact, I rarely ever place code between <script></script> tags, and NEVER place code into regular HTML tags. I also never place events into HTML tags, not even the <body> tag. This can all be done from an external file.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 06-30-2008, 03:22 PM Re: getElementById from external document
Novice Talker

Posts: 11
Trades: 0
Quote:
Originally Posted by wayfarer07 View Post
I also never place events into HTML tags, not even the <body> tag. This can all be done from an external file.
That sounds good. Could you tell us how do you do this(code)?
AssaultM16 is offline
Reply With Quote
View Public Profile
 
Old 06-30-2008, 05:11 PM Re: getElementById from external document
nyef's Avatar
Ultra Talker

Posts: 265
Name: Lucas
Trades: 0
Wouldn't it be a better idea to move the code that checks the user's status to closer to the start of the page so that it would work normally? Rather than trying a workaround I always prefer to fix the issue at hand...
__________________
~nyef

Please login or register to view this content. Registration is FREE
nyef is offline
Reply With Quote
View Public Profile Visit nyef's homepage!
 
Old 07-01-2008, 02:38 AM Re: getElementById from external document
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Quote:
Originally Posted by AssaultM16 View Post
That sounds good. Could you tell us how do you do this(code)?
Some examples:
Code:
document.body.onload = myFunction;
document.getElementById('example_element_id').onclick = testFunction;

function myFunction(evt) {
   alert('body completed loading');
}

function testFunction(evt) {
   alert('element just received an ' + evt.type);
}
That's just one way to do it. There are a couple of other ways to set event handlers as well.

I highly recommend Quirksmode for more information on JS events. LINK
frost is offline
Reply With Quote
View Public Profile
 
Old 07-01-2008, 08:06 PM Re: getElementById from external document
Novice Talker

Posts: 11
Trades: 0
Thank you Frost!
AssaultM16 is offline
Reply With Quote
View Public Profile
 
Old 07-05-2008, 07:00 PM Re: getElementById from external document
Junior Talker

Posts: 3
Name: john anderson
Trades: 0
The simplest way is to use a JDOMP as outlined below.


You can simply change text on a page using the DOM without using innerHTML.

<DIV id=”mytext”>Loading…….</DIV>

<A id="mymod" href="javascript:textmod('mytext', 'the count is 5')">Change Counter</A>

<SCRIPT type="text/javascript" >
function textmod (elid, newtext) {
// get reference to the element
var ttext = document.getElementById(elid);
var new_txt = document.createTextNode(newtext);
ttext.replaceChild(new_txt, ttext.childNodes[0]);
}
</SCRIPT>

Now to look at how to change text with data extracted from a database.
Let’s say you have a counter on the page and when the page loads you want to go to a PHP file update the counter and send back the data and change text on the page.
You start by adding this to the bottom of the HTML.

<SCRIPT type="text/javascript" SRC="mylinkcount.php "></SCRIPT>

For the PHP file - mylinkcount.php
<?php
Blah, blah, blah access database etc.
// out info
$div = "mytext";
$count = $clicks." - Visitors since 1 May 2008";
echo " textmod('$div','$count');";
?>
You simply echo the javascript command with the variables. This will change the text on the page a second or so after the page loads. This is a JDOMP!

To do this dynamically you need to use the DOM to dynamically build the script command as above.

Let’s say we have a form and we want to show the information extracted from a database.

<form id="form1" name="form1">
Id: &nbsp;
<input id= "myid" name="myid" type="text" value="1" >
Word: &nbsp;
<input id= "myword" name="myword" type="text" value="First Word" >
</form>

This link will inset blank values
<a href="javascript:clear()">Clear Me</A>
Using these functions

function clear() {
listmake(" ", " ")
}

function listmake(gotid, gotword) {
document.forms[0].myid.value = gotid
document.forms[0].myword.value = gotword
}

To load data into the form dynamically from a database you need a container and a link

<DIV id="List">
<DIV id="Listp"></DIV>
</DIV>

<a href="javascript:getme(23)">Get Me 23</A> …. Get the word with id ‘23’

And the function to build the script

function getme(mynum) {
var d = new Date();
var time = d.getTime();
var mynurl = "gme.php?myvar=" + myid + " &time = " + time
// url for php program with myid as the variable
// + time stamp added to avoid cache problems
//the following replaces Lisp with the script
var mydiv = document.getElementById("List");
var myoldp = document.getElementById("Listp");
var mynewp = document.createElement("DIV")
mynewp.id="Listp";
var docfrag = document.createDocumentFragment();
myelem = document.createElement("script")
docfrag.appendChild(myelem);
myelem.setAttribute("type","text/javascript")
myelem.setAttribute("src",mynurl)
docfrag.appendChild(myelem);

mynewp.appendChild(docfrag);
mydiv.replaceChild(mynewp, myoldp)
}
It looks complicated but it’s a reusable function so once it works you can forget the details.

And the PHP file - gme.php

<?php
Blah, blah, blah access database etc.
// out info
$id = parseToXML($row['id']);
$word = parseToXML($row['word']);
echo " listmake('$id','$word');";
?>
Once again you simply echo the javascript command and Voila! the data is transferred to the form.

Anyway that’s it. Try JDOMP and let me know what you think.

Cheers,
JDOMper is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to getElementById from external document
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.25964 seconds with 12 queries