JavaScript setAttribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Phillips

    JavaScript setAttribute

    Hope someone can help.

    I am struggling to write an meta element in JS. The meta element in question
    is the refresh element.

    This is the code I am using

    //--------------------------------------------------------------------------
    ----
    function setMetaContent( metaTag, metaName, value) {
    //--------------------------------------------------------------------------
    ----
    var metas = document.getEle mentsByTagName( 'meta');

    if (debug) alert('[setMetaContent] metaTag='+metaT ag+' metaName=
    '+metaName+' value '+value);
    for (var i=0; i<metas.length ; i++) {
    if (metas[i].getAttribute(m etaTag)==metaNa me)
    break;
    }
    if (debug) alert('[setMetaContent] i='+i+' value '+value);
    metas[i].setAttribute(' content',value) ;
    }


    and I call it with

    var metaValue = '10;url='+rootS ource+fileBase+ aryChile[nImgId][4]+'.html';
    if (debug) alert('[writeIMG] metaValue ='metaValue);
    setMetaContent( 'http-equiv','refresh ',metaValue);

    where rootSource = "../source/", fileBase = "picture", and
    aryChile[nImgId][4] resolves to an integer . The url setup looks okay, but
    the code seems to disappear intgo oblivion.

    Two questions
    1) can I set a meta element in this way
    2) does the code look correct?

    TIA

    Bob


  • RobG

    #2
    Re: JavaScript setAttribute

    Bob Phillips wrote:[color=blue]
    > Hope someone can help.
    >
    > I am struggling to write an meta element in JS. The meta element in question
    > is the refresh element.
    >
    > This is the code I am using
    >
    > //--------------------------------------------------------------------------
    > ----
    > function setMetaContent( metaTag, metaName, value) {
    > //--------------------------------------------------------------------------
    > ----
    > var metas = document.getEle mentsByTagName( 'meta');
    >
    > if (debug) alert('[setMetaContent] metaTag='+metaT ag+' metaName=
    > '+metaName+' value '+value);
    > for (var i=0; i<metas.length ; i++) {
    > if (metas[i].getAttribute(m etaTag)==metaNa me)
    > break;
    > }
    > if (debug) alert('[setMetaContent] i='+i+' value '+value);
    > metas[i].setAttribute(' content',value) ;
    > }
    >
    >
    > and I call it with
    >
    > var metaValue = '10;url='+rootS ource+fileBase+ aryChile[nImgId][4]+'.html';
    > if (debug) alert('[writeIMG] metaValue ='metaValue);
    > setMetaContent( 'http-equiv','refresh ',metaValue);
    >
    > where rootSource = "../source/", fileBase = "picture", and
    > aryChile[nImgId][4] resolves to an integer . The url setup looks okay, but
    > the code seems to disappear intgo oblivion.
    >
    > Two questions
    > 1) can I set a meta element in this way[/color]

    I doubt it. You appear to be trying to modify an external file, if so,
    then absolutely not (unless you are using some IE extension, HTA, etc.
    that allows it).

    [color=blue]
    > 2) does the code look correct?[/color]

    No. The 'http-equiv' property of a meta tag is called 'httpEquiv'
    (ignoring the external file access issue).

    I'm not sure that modifying the value of the refresh property of meta
    element has any effect. Seems to me that once the browser has parsed
    it on page load, modifying it doesn't change the refresh rate.

    Here's a little script to see what the refresh rate is:

    <script type="text/javascript">

    var metas = document.getEle mentsByTagName( 'meta');
    for (var i=0; i<metas.length ; i++){
    if ( 'refresh' == metas[i].httpEquiv.toLo werCase() ){
    alert(metas[i].content);
    }
    }

    </script>

    --
    Rob

    Comment

    • Bob Phillips

      #3
      Re: JavaScript setAttribute


      "RobG" <rgqld@iinet.ne t.auau> wrote in message
      news:M_Sge.1430 $Zn.71356@news. optus.net.au...[color=blue]
      >
      > I doubt it. You appear to be trying to modify an external file, if so,
      > then absolutely not (unless you are using some IE extension, HTA, etc.
      > that allows it).[/color]

      I am not trying to modify a file, just change the URL that the refresh will
      redirect to. The background is that I have a slideshow, and it automatically
      redirects after 10 secs. Now there are 300 items in this slideshow, and as
      each redirects to a different page, that is 300 HTMl pages to maintain. I
      hoped to do it by one page, replicated, that got its information at run time
      to determine the redirect page (I can't use server side code, has to be
      client on this).[color=blue]
      >[color=green]
      > > 2) does the code look correct?[/color]
      >
      > No. The 'http-equiv' property of a meta tag is called 'httpEquiv'
      > (ignoring the external file access issue).
      >
      > I'm not sure that modifying the value of the refresh property of meta
      > element has any effect. Seems to me that once the browser has parsed
      > it on page load, modifying it doesn't change the refresh rate.[/color]

      As I say, the rate is of no interest, just the URL, and I am doing this as
      it loads.

      Any other way to get what I need?


      Comment

      • RobG

        #4
        Re: JavaScript setAttribute

        Bob Phillips wrote:[color=blue]
        > "RobG" <rgqld@iinet.ne t.auau> wrote in message
        > news:M_Sge.1430 $Zn.71356@news. optus.net.au...
        >[color=green]
        >> I doubt it. You appear to be trying to modify an external file, if so,
        >> then absolutely not (unless you are using some IE extension, HTA, etc.
        >> that allows it).[/color]
        >
        >
        > I am not trying to modify a file, just change the URL that the refresh will
        > redirect to.[/color]

        The HTML 4 spec says:

        "Note. Some user agents support the use of META to refresh the
        current page after a specified number of seconds, with the option
        of replacing it by a different URI. Authors should not use this
        technique to forward users to different pages, as this makes the
        page inaccessible to some users. Instead, automatic page forwarding
        should be done using server-side redirects."

        So you shouldn't be using a meta element for this.
        [color=blue]
        > redirect to. The background is that I have a slideshow, and it automatically
        > redirects after 10 secs. Now there are 300 items in this slideshow, and as
        > each redirects to a different page, that is 300 HTMl pages to maintain. I
        > hoped to do it by one page, replicated, that got its information at run time
        > to determine the redirect page (I can't use server side code, has to be
        > client on this).
        >[/color]

        [...]
        [color=blue]
        > As I say, the rate is of no interest, just the URL, and I am doing this as
        > it loads.
        >
        > Any other way to get what I need?
        >[/color]


        Why not use setTimeout to change document.locati on.href at your chose
        interval? The 'slideshow' won't work for non-JavaScript browsers,
        whether that suits or not is up to you.

        Put the following into an external file called 'changeURL.js', put
        the HTML into files called 0.html and 1.html. It will go between
        them every 2 seconds. Change as required.

        I've only been able to test this in Safari, so test thoroughly.


        /******* changeURL.js *******/

        /* changeURL
        * Changes files from 0.html to 'numfiles'.html
        * at intervals of 'lag'
        */
        function changeURL() {
        var numFiles = 2;
        var b = String(document .URL);

        // Get filename of current page
        var f = b.replace(/.*[/\\\\]/,'');

        // Increment the filename & build new name
        var g = f.split('.');
        g = (+g[0] + 1)%numFiles + '.' + g[1];

        // Change the URL
        document.locati on.href = b.replace(f,'') + g;

        }

        setTimeout("cha ngeURL()",2000) ;


        /******* 0.html *******/

        <html>
        <head><title>Sl ide show 0</title>

        <script type="text/javascript" src="changeURL. js"></script>

        </head>
        <body>
        <h1>Page 0</h1>
        </body>
        </html>

        /******* 0.html *******/

        <html>
        <head><title>Sl ide show 1</title>

        <script type="text/javascript" src="changeURL. js"></script>

        </head>
        <body>
        <h1>Page 1</h1>
        </body>
        </html>



        --
        Rob

        Comment

        • Bob Phillips

          #5
          Re: JavaScript setAttribute

          Rob,

          Thanks for that, the suggestion looks interesting, and I have tried to
          implement a modified version, but I have a problem.

          What I am doing is to work out the next page to load and storing it into a
          variable called sNextFile, and setting a timer like so

          setTimeout("cha ngeURL('"+sNext File+"')",5000) ;

          Here is my changeURL routine, very simple

          //--------------------------------------------------------------------------
          ----
          function changeURL(inURL ) {
          //--------------------------------------------------------------------------
          ----

          if (debug) alert('[changeURL] '+inURL)
          document.locati on.href = inURL;

          }

          Problem is that when I pass a value to the function, it strips the / from
          the value. So although I have the value

          file://D:\Bob\web\nahr um\source\pictu re2.html

          but in the function, the value seen is

          file://D:Bobwebnahrums ourcepicture2.h tml

          so of course the page is not found. Any idea what is going on?

          TIA

          Bob

          "RobG" <rgqld@iinet.ne t.auau> wrote in message
          news:42848de7$0 $7199$5a62ac22@ per-qv1-newsreader-01.iinet.net.au ...[color=blue]
          > Bob Phillips wrote:[color=green]
          > > "RobG" <rgqld@iinet.ne t.auau> wrote in message
          > > news:M_Sge.1430 $Zn.71356@news. optus.net.au...
          > >[color=darkred]
          > >> I doubt it. You appear to be trying to modify an external file, if[/color][/color][/color]
          so,[color=blue][color=green][color=darkred]
          > >> then absolutely not (unless you are using some IE extension, HTA, etc.
          > >> that allows it).[/color]
          > >
          > >
          > > I am not trying to modify a file, just change the URL that the refresh[/color][/color]
          will[color=blue][color=green]
          > > redirect to.[/color]
          >
          > The HTML 4 spec says:
          >
          > "Note. Some user agents support the use of META to refresh the
          > current page after a specified number of seconds, with the option
          > of replacing it by a different URI. Authors should not use this
          > technique to forward users to different pages, as this makes the
          > page inaccessible to some users. Instead, automatic page forwarding
          > should be done using server-side redirects."
          >
          > So you shouldn't be using a meta element for this.
          >[color=green]
          > > redirect to. The background is that I have a slideshow, and it[/color][/color]
          automatically[color=blue][color=green]
          > > redirects after 10 secs. Now there are 300 items in this slideshow, and[/color][/color]
          as[color=blue][color=green]
          > > each redirects to a different page, that is 300 HTMl pages to maintain.[/color][/color]
          I[color=blue][color=green]
          > > hoped to do it by one page, replicated, that got its information at run[/color][/color]
          time[color=blue][color=green]
          > > to determine the redirect page (I can't use server side code, has to be
          > > client on this).
          > >[/color]
          >
          > [...]
          >[color=green]
          > > As I say, the rate is of no interest, just the URL, and I am doing this[/color][/color]
          as[color=blue][color=green]
          > > it loads.
          > >
          > > Any other way to get what I need?
          > >[/color]
          >
          >
          > Why not use setTimeout to change document.locati on.href at your chose
          > interval? The 'slideshow' won't work for non-JavaScript browsers,
          > whether that suits or not is up to you.
          >
          > Put the following into an external file called 'changeURL.js', put
          > the HTML into files called 0.html and 1.html. It will go between
          > them every 2 seconds. Change as required.
          >
          > I've only been able to test this in Safari, so test thoroughly.
          >
          >
          > /******* changeURL.js *******/
          >
          > /* changeURL
          > * Changes files from 0.html to 'numfiles'.html
          > * at intervals of 'lag'
          > */
          > function changeURL() {
          > var numFiles = 2;
          > var b = String(document .URL);
          >
          > // Get filename of current page
          > var f = b.replace(/.*[/\\\\]/,'');
          >
          > // Increment the filename & build new name
          > var g = f.split('.');
          > g = (+g[0] + 1)%numFiles + '.' + g[1];
          >
          > // Change the URL
          > document.locati on.href = b.replace(f,'') + g;
          >
          > }
          >
          > setTimeout("cha ngeURL()",2000) ;
          >
          >
          > /******* 0.html *******/
          >
          > <html>
          > <head><title>Sl ide show 0</title>
          >
          > <script type="text/javascript" src="changeURL. js"></script>
          >
          > </head>
          > <body>
          > <h1>Page 0</h1>
          > </body>
          > </html>
          >
          > /******* 0.html *******/
          >
          > <html>
          > <head><title>Sl ide show 1</title>
          >
          > <script type="text/javascript" src="changeURL. js"></script>
          >
          > </head>
          > <body>
          > <h1>Page 1</h1>
          > </body>
          > </html>
          >
          >
          >
          > --
          > Rob[/color]


          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: JavaScript setAttribute

            Bob Phillips wrote:
            [color=blue]
            > Problem is that when I pass a value to the function, it strips the / from
            > the value. So although I have the value
            >
            > file://D:\Bob\web\nahr um\source\pictu re2.html[/color]

            The proper URI is

            file:///D:/Bob/web/nahrum/source/picture2.html

            which is the same as

            file://localhost/D:/Bob/web/nahrum/source/picture2.html
            [color=blue]
            > but in the function, the value seen is
            >
            > file://D:Bobwebnahrums ourcepicture2.h tml
            >
            > so of course the page is not found. Any idea what is going on?[/color]

            The backslash (`\') acts as escape character in string and RegExp
            literals. You have to escape it to have it displayed: \\ (nothing new)
            [color=blue]
            > [Top post][/color]

            <http://jibbering.com/faq/>


            PointedEars

            Comment

            • Bob Phillips

              #7
              Re: JavaScript setAttribute


              "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
              news:3261765.Xr Ie83BsLs@Pointe dEars.de...[color=blue]
              > Bob Phillips wrote:
              >[color=green]
              > > Problem is that when I pass a value to the function, it strips the /[/color][/color]
              from[color=blue][color=green]
              > > the value. So although I have the value
              > >
              > > file://D:\Bob\web\nahr um\source\pictu re2.html[/color]
              >
              > The proper URI is
              >
              > file:///D:/Bob/web/nahrum/source/picture2.html
              >[/color]

              But I get that value by issuing a

              var thisURL = String(document .URL);

              in the JS!


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: JavaScript setAttribute

                Bob Phillips wrote:
                [color=blue]
                > "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote [...]:[color=green]
                >> Bob Phillips wrote:[color=darkred]
                >> > Problem is that when I pass a value to the function, it strips the /[/color][/color]
                > from[/color]
                ^^^^
                Please follow the recommendations at <http://insideoe.tomste rdam.com/>
                to avoid further borken quotes.
                [color=blue][color=green][color=darkred]
                >> > the value. So although I have the value
                >> >
                >> > file://D:\Bob\web\nahr um\source\pictu re2.html[/color]
                >>
                >> The proper URI is
                >>
                >> file:///D:/Bob/web/nahrum/source/picture2.html[/color]
                >
                > But I get that value by issuing a
                >
                > var thisURL = String(document .URL);[/color]

                The value of `document.URL' is already a string, it does not need to be
                "stringifie d". Furthermore, you should use `location' or `location.href'
                instead; `document.locat ion' and its IE version `document.URL' is
                deprecated long since. And if even that will not help, you should
                convert backslashes to forward slashes before you continue:

                var thisURL = location.href.r eplace(/\\/g, "/");


                PointedEars

                Comment

                Working...