Coding problem

Discussion in 'HTML' started by Robert Baer, Aug 22, 2013.

  1. Robert Baer

    Robert Baer Guest

    The following code "works" in a strange way; see the comments for
    details.
    1) why is that document.write necessary for operation?
    2) why doesn't the altered variable "title" remain changed?
    3) i would like to change displayed page, based on map visits

    Help? Comments?


    <SCRIPT type="text/javascript">
    // <img id="page" src=title alt="Codatron page 1">
    // src="images/CodatronP1.png"
    var title="images/CodatronP1.png";
    var dir=-1;
    var num=3;
    function incrPage(dir,title)
    {
    num=title.substr(16,1).valueOf();
    if (dir===-1)
    {
    num=num-1;
    if (num===0) {num=7;}
    }
    else
    {
    num=num+1;
    if (num===8) {num=1;}
    }
    title=title.substr(0,16)+num+title.substr(17);
    document.write("###"+title+"*");
    // above correct at page 7 being "previous" to page 1 (sequence 1,2,,7,1
    etc)
    // *NOTE* the document.write MUST exist for the map visits to work ???
    }
    incrPage(dir,title);
    // document.write("***"+title+"* *#");
    // above is wrong, with title equal to unchanged initial def

    // document.getElementById("page").innerHTML=title;
    </script>

    <div>
    <img src="ShuttleN.png" alt="Select" usemap="#PageSelect"
    </div>
    <div>
    <!-- visiting mapped areas give "###-1*" or "###1*" in new screen -->
    <map id="images/CodatronP5.png" name="PageSelect">
    <area shape="rect" alt="Minus" coords="65,0,135,35"
    src="N.PNG" onMouseover="incrPage(-1,id)" >
    <area shape="rect" alt="Plus" coords="590,0,668,35"
    src="P.PNG" onMouseover="incrPage(1,id)" >
    <area shape="default" nohref="nohref" alt="" >
    </map>
    </div>

    <!-- display page one; would like to change that based on map visits -->
    <div class=" container_24">
    <div class="wrapper">
    <img id="page" src="images/CodatronP1.png" alt="Page">
    </div>
    </div>
    Robert Baer, Aug 22, 2013
    #1
    1. Advertising

  2. Robert Baer

    Doug Miller Guest

    Robert Baer <> wrote in news:DptRt.321878$:

    > The following code "works" in a strange way; see the comments for
    > details.
    > 1) why is that document.write necessary for operation?
    > 2) why doesn't the altered variable "title" remain changed?
    > 3) i would like to change displayed page, based on map visits
    >
    > Help? Comments?


    You'll probably have better luck posting in one of the javascript groups.
    Doug Miller, Aug 22, 2013
    #2
    1. Advertising

  3. Robert Baer <> writes:

    > Doug Miller wrote:
    >> Robert Baer<> wrote in news:DptRt.321878$:
    >>
    >>> The following code "works" in a strange way; see the comments for
    >>> details.
    >>> 1) why is that document.write necessary for operation?
    >>> 2) why doesn't the altered variable "title" remain changed?
    >>> 3) i would like to change displayed page, based on map visits
    >>>
    >>> Help? Comments?

    >>
    >> You'll probably have better luck posting in one of the javascript groups.

    > I have found only one JS group, and it is essentially dead.
    > Hopefully, http://www.codingforums.com can help.
    > Thanks.


    comp.lang.javascript is a possibility.

    And your questions:

    1) It's not, but it's one reasonable way to inject some text into an
    existing document.

    2) This is a basic property functions and arguments. Argument passing
    is by copying:

    vat title = "abc";

    function test1(t) { t = "def"; }
    function test2(title) { title = "def"; }

    test1(title);
    test2(title);
    // Both functions are essentially the same and neither call changes
    // the value of the "outer" variable called title.

    If you need a changed value, get the function to return it.

    title = calculateNewTitle(title);

    3) Pass

    --
    Ben.
    Ben Bacarisse, Aug 22, 2013
    #3
  4. Robert Baer

    Robert Baer Guest

    Doug Miller wrote:
    > Robert Baer<> wrote in news:DptRt.321878$:
    >
    >> The following code "works" in a strange way; see the comments for
    >> details.
    >> 1) why is that document.write necessary for operation?
    >> 2) why doesn't the altered variable "title" remain changed?
    >> 3) i would like to change displayed page, based on map visits
    >>
    >> Help? Comments?

    >
    > You'll probably have better luck posting in one of the javascript groups.

    I have found only one JS group, and it is essentially dead.
    Hopefully, http://www.codingforums.com can help.
    Thanks.
    Robert Baer, Aug 23, 2013
    #4
  5. Robert Baer

    Robert Baer Guest

    Doug Miller wrote:
    > javascript groups.

    There are no functional javascript groups.
    Robert Baer, Aug 25, 2013
    #5
  6. Robert Baer

    Robert Baer Guest

    Ben Bacarisse wrote:
    > Robert Baer<> writes:
    >
    >> Doug Miller wrote:
    >>> Robert Baer<> wrote in news:DptRt.321878$:
    >>>
    >>>> The following code "works" in a strange way; see the comments for
    >>>> details.
    >>>> 1) why is that document.write necessary for operation?
    >>>> 2) why doesn't the altered variable "title" remain changed?
    >>>> 3) i would like to change displayed page, based on map visits
    >>>>
    >>>> Help? Comments?
    >>>
    >>> You'll probably have better luck posting in one of the javascript groups.

    >> I have found only one JS group, and it is essentially dead.
    >> Hopefully, http://www.codingforums.com can help.
    >> Thanks.

    >
    > comp.lang.javascript is a possibility.

    * Not functional, been empty for ages.

    >
    > And your questions:
    >
    > 1) It's not, but it's one reasonable way to inject some text into an
    > existing document.

    * Function does not work without that line of code. I agree, it should.
    The "return" statement oes nothing except guarantee that JS proceeds
    no further (and that agrees with the documentation as far as execution
    is concerned).

    >
    > 2) This is a basic property functions and arguments. Argument passing
    > is by copying:
    >
    > vat title = "abc";
    >
    > function test1(t) { t = "def"; }
    > function test2(title) { title = "def"; }
    >
    > test1(title);
    > test2(title);
    > // Both functions are essentially the same and neither call changes
    > // the value of the "outer" variable called title.
    >
    > If you need a changed value, get the function to return it.
    >
    > title = calculateNewTitle(title);

    * Oh! Thanks; the online "tutorial" does not give refinements like that.

    >
    > 3) Pass
    >

    2 out of 3 is excellent!
    Thanks.
    Robert Baer, Aug 25, 2013
    #6
  7. Robert Baer

    Robert Baer Guest

    Robert Baer wrote:
    > Doug Miller wrote:
    >> Robert Baer<> wrote in
    >> news:DptRt.321878$:
    >>
    >>> The following code "works" in a strange way; see the comments for
    >>> details.
    >>> 1) why is that document.write necessary for operation?
    >>> 2) why doesn't the altered variable "title" remain changed?
    >>> 3) i would like to change displayed page, based on map visits
    >>>
    >>> Help? Comments?

    >>
    >> You'll probably have better luck posting in one of the javascript groups.

    > I have found only one JS group, and it is essentially dead.
    > Hopefully, http://www.codingforums.com can help.
    > Thanks.
    >

    Well, if i can only figure out how to POST after the preview...
    Anyone have a clue?
    Robert Baer, Aug 25, 2013
    #7
  8. Robert Baer <> writes:

    > Ben Bacarisse wrote:
    >> Robert Baer<> writes:
    >>
    >>> Doug Miller wrote:
    >>>> Robert Baer<> wrote in news:DptRt.321878$:
    >>>>
    >>>>> The following code "works" in a strange way; see the comments for
    >>>>> details.
    >>>>> 1) why is that document.write necessary for operation?
    >>>>> 2) why doesn't the altered variable "title" remain changed?
    >>>>> 3) i would like to change displayed page, based on map visits
    >>>>>
    >>>>> Help? Comments?
    >>>>
    >>>> You'll probably have better luck posting in one of the javascript groups.
    >>> I have found only one JS group, and it is essentially dead.
    >>> Hopefully, http://www.codingforums.com can help.
    >>> Thanks.

    >>
    >> comp.lang.javascript is a possibility.

    > * Not functional, been empty for ages.


    I posted a reply there yesterday. I see messages there every day, more
    or less, -- more that I see here, for example.

    >> And your questions:
    >>
    >> 1) It's not, but it's one reasonable way to inject some text into an
    >> existing document.

    > * Function does not work without that line of code. I agree, it should.
    > The "return" statement oes nothing except guarantee that JS proceeds
    > no further (and that agrees with the documentation as far as execution
    > is concerned).


    Misunderstanding here. When I said that the document.write was not
    necessary I meant that there are other ways to do the kind of thing that
    document.write does. The specific question (which is what I think you
    were asking) of why the code works with it, and does not work without
    it, seems unanswerable without more data. The context of the code,
    and the definition of "works" and "not works" being the main things I'd
    want to know. I agree it looks odd, but that's all I can say.

    <snip>
    --
    Ben.
    Ben Bacarisse, Aug 25, 2013
    #8
  9. Robert Baer

    Robert Baer Guest

    Hot-Text wrote:
    > "Robert Baer" <> wrote in message
    > news:dltSt.150347$...
    >> Robert Baer wrote:
    >>> Doug Miller wrote:
    >>>> Robert Baer<> wrote in
    >>>> news:DptRt.321878$:
    >>>>> The following code "works" in a strange way; see the comments for
    >>>>> details.
    >>>>> 1) why is that document.write necessary for operation?
    >>>>> 2) why doesn't the altered variable "title" remain changed?
    >>>>> 3) i would like to change displayed page, based on map visits
    >>>>> Help? Comments?
    >>>> You'll probably have better luck posting in one of the javascript
    >>>> groups.
    >>> I have found only one JS group, and it is essentially dead.
    >>> Hopefully, http://www.codingforums.com can help.
    >>> Thanks.

    >> Well, if i can only figure out how to POST after the preview...
    >> Anyone have a clue?
    >>

    >
    > Same more info for
    > < http://www.freewebmasterhelp.com/tutorials/javascript >

    Wow! Nicely done, good presentation, nice examples.
    Tanks a lot!!
    Robert Baer, Aug 26, 2013
    #9
  10. Robert Baer

    Robert Baer Guest

    Hot-Text wrote:
    > "Robert Baer" <> wrote in message
    > news:aOMSt.184242$...
    >> Hot-Text wrote:
    >>> "Robert Baer" <> wrote in message
    >>> news:dltSt.150347$...
    >>>> Robert Baer wrote:
    >>>>> Doug Miller wrote:
    >>>>>> Robert Baer<> wrote in
    >>>>>> news:DptRt.321878$:
    >>>>>>> The following code "works" in a strange way; see the comments for
    >>>>>>> details.
    >>>>>>> 1) why is that document.write necessary for operation?
    >>>>>>> 2) why doesn't the altered variable "title" remain changed?
    >>>>>>> 3) i would like to change displayed page, based on map visits
    >>>>>>> Help? Comments?
    >>>>>> You'll probably have better luck posting in one of the javascript
    >>>>>> groups.
    >>>>> I have found only one JS group, and it is essentially dead.
    >>>>> Hopefully, http://www.codingforums.com can help.
    >>>>> Thanks.
    >>>> Well, if i can only figure out how to POST after the preview...
    >>>> Anyone have a clue?
    >>> Same more info for
    >>> < http://www.freewebmasterhelp.com/tutorials/javascript >

    >> Wow! Nicely done, good presentation, nice examples.
    >> Tanks a lot!!
    >>

    >
    > You're welcome
    >
    > I was doing a Internet Archive: Wayback Machine
    > Looking for old Editor Software back in 1998
    > and one of the websites had a like to freewebmasterhelp
    > and I saw the JavaScript and remember you
    >
    >

    With that reference, and a few hours of fiddling, i am _close_ to
    getting what i want.
    Thanx for the help!

    Have seven images and want to cycle thru them + for 7,1,2,3,,6,7 and
    - for 1,7,6,5,,2,1.
    Using HTML usemap and specific image names, + increments its given
    name in that part of code, and - decrements its given name in that part
    of code; both work nicely.
    Used two JS functions; crudely first called via mouseOver(), one
    presents a given name and calls second that properly cycles thru the
    numbers/names.
    But it "steps" only once.
    I think that if there was such a beastie as a HTML variable, i could
    solve this in short order.

    Got the code and query posted in codingforums.com .
    Robert Baer, Aug 27, 2013
    #10
  11. Robert Baer

    meagain Guest

    Robert Baer wrote:
    > Hot-Text wrote:
    >> "Robert Baer" <> wrote in message
    >> news:dltSt.150347$...
    >>> Robert Baer wrote:
    >>>> Doug Miller wrote:
    >>>>> Robert Baer<> wrote in
    >>>>> news:DptRt.321878$:
    >>>>>> The following code "works" in a strange way; see the comments for
    >>>>>> details.
    >>>>>> 1) why is that document.write necessary for operation?
    >>>>>> 2) why doesn't the altered variable "title" remain changed?
    >>>>>> 3) i would like to change displayed page, based on map visits
    >>>>>> Help? Comments?
    >>>>> You'll probably have better luck posting in one of the javascript
    >>>>> groups.
    >>>> I have found only one JS group, and it is essentially dead.
    >>>> Hopefully, http://www.codingforums.com can help.
    >>>> Thanks.
    >>> Well, if i can only figure out how to POST after the preview...
    >>> Anyone have a clue?
    >>>

    >>
    >> Same more info for
    >> < http://www.freewebmasterhelp.com/tutorials/javascript >

    > Wow! Nicely done, good presentation, nice examples.
    > Tanks a lot!!
    >


    It is good, but does not directly answer the Original question
    "why is document.write necessary."

    Examine some javascript code, like
    document.write(
    "<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
    );

    - http://96.9.49.162/AccessBolton_files/NowPlaying.htm

    and you should see why it is "necessary."
    meagain, Aug 27, 2013
    #11
  12. meagain wrote:

    >> > Robert Baer wrote:

    > Hot-Text wrote:



    >>> Same more info for
    >>> < http://www.freewebmasterhelp.com/tutorials/javascript >

    >> Wow! Nicely done, good presentation, nice examples.
    >> Tanks a lot!!
    >>


    The above link is obsolete at best, recommending such things as HTML
    comments in script elements to "hide script from old browsers". Really?
    If memory serves me wasn't that a version of Netscape 2?

    >
    > It is good, but does not directly answer the Original question
    > "why is document.write necessary."
    >
    > Examine some javascript code, like
    > document.write(
    > "<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
    > );
    >
    > - http://96.9.49.162/AccessBolton_files/NowPlaying.htm
    >
    > and you should see why it is "necessary."


    "necessary"? The main problem with document.write is that it must be
    done BEFORE the document finishes loading whereas using DOM you can add,
    remove, and modify elements after the document loads.

    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO
    http://www.LittleWorksStudio.com
    Jonathan N. Little, Aug 27, 2013
    #12
  13. Robert Baer

    Robert Baer Guest

    meagain wrote:
    > Robert Baer wrote:
    >> Hot-Text wrote:
    >>> "Robert Baer" <> wrote in message
    >>> news:dltSt.150347$...
    >>>> Robert Baer wrote:
    >>>>> Doug Miller wrote:
    >>>>>> Robert Baer<> wrote in
    >>>>>> news:DptRt.321878$:
    >>>>>>> The following code "works" in a strange way; see the comments for
    >>>>>>> details.
    >>>>>>> 1) why is that document.write necessary for operation?
    >>>>>>> 2) why doesn't the altered variable "title" remain changed?
    >>>>>>> 3) i would like to change displayed page, based on map visits
    >>>>>>> Help? Comments?
    >>>>>> You'll probably have better luck posting in one of the javascript
    >>>>>> groups.
    >>>>> I have found only one JS group, and it is essentially dead.
    >>>>> Hopefully, http://www.codingforums.com can help.
    >>>>> Thanks.
    >>>> Well, if i can only figure out how to POST after the preview...
    >>>> Anyone have a clue?
    >>>>
    >>>
    >>> Same more info for
    >>> < http://www.freewebmasterhelp.com/tutorials/javascript >

    >> Wow! Nicely done, good presentation, nice examples.
    >> Tanks a lot!!
    >>

    >
    > It is good, but does not directly answer the Original question
    > "why is document.write necessary."
    >
    > Examine some javascript code, like
    > document.write(
    > "<div>"+Date().toLocaleString().substr(0,10).fontsize(6).bold()+txt.fontsize(6).bold()+"<br><br><\/div>"
    > );
    >
    > - http://96.9.49.162/AccessBolton_files/NowPlaying.htm
    >
    > and you should see why it is "necessary."

    Finally found an un-documented trick by using
    document.getElementById() "backwards".
    Do not need document.write anywhere.
    Am cycling pictures forward or backwards according to onMouseover
    visits at 2 designated places.
    Code:
    <script type="text/javascript">
    function incrPage(dir,title)
    {
    var num=+title.substr(16,1).valueOf();
    if (dir===-1)
    {
    num=num-1;
    if (num===0) {num=7;}
    }
    else
    {
    num=num+1;
    if (num===8) {num=1;}
    }
    title=title.substr(0,16)+num+title.substr(17);
    return title;
    // Sequence is 1,2,,7,1 for plus, 1,7,6,,1 for minus
    }
    
    function SwPage(updn) {
    var Pnam=document.getElementById("imgpage").src;
    // above gives full URL, so must parse
    var NamLoc=Pnam.search("images");
    var ISnam=Pnam.substr(NamLoc);
    var NUpg="";
    if (updn ==="up") {
    NUpg=incrPage(1,ISnam);
    document.getElementById("imgpage").src = NUpg;
    }
    else {
    NUpg=incrPage(-1,ISnam);
    document.getElementById("imgpage").src = NUpg;
    }
    }
    </script>
    
    Robert Baer, Aug 28, 2013
    #13
  14. Robert Baer

    Tim Streater Guest

    In article <pgeTt.73700$>,
    Robert Baer <> wrote:

    [snip]

    > Finally found an un-documented trick by using
    > document.getElementById() "backwards".


    [snip]

    > document.getElementById("imgpage").src = NUpg;


    if you're referring to this, it's not a trick and it's not
    "undocumented", it's SOP.

    It's the same as doing this:

    ptr = document.getElementById("imgpage");
    ptr.scr = NUpg;

    just that with what you wrote you get a pointer to the element when you
    need it rather than ahead of time. There is some optimisation advantage
    to my code if you are going to use this pointer lots of times.

    --
    Tim

    "That excessive bail ought not to be required, nor excessive fines imposed,
    nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
    Tim Streater, Aug 28, 2013
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. smu
    Replies:
    3
    Views:
    2,629
    shobhit24
    Jun 28, 2006
  2. Bart Schelkens

    Problem with coding

    Bart Schelkens, Jul 28, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    308
    Yuk Cheung
    Jul 29, 2004
  3. Jason

    simple jsp coding problem

    Jason, Sep 8, 2003, in forum: Java
    Replies:
    1
    Views:
    414
    Joe Smith
    Sep 8, 2003
  4. calmar
    Replies:
    11
    Views:
    756
    calmar
    Feb 21, 2006
  5. richard
    Replies:
    13
    Views:
    957
    Doug Miller
    Apr 24, 2012
Loading...

Share This Page