Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25
  1. #16
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts

    New version of my code

    I used what i am comfortable with and can understand fully.
    You are welcome to critique and or comment.
    Code:
    function incrPage(dir,nampg)
     {
      var num=+nampg.valueOf();
      if (dir===-1)
       {
       num=num-1;
       if (num===0) {num=7;}
       }
      else
       {
       num=num+1;
       if (num===8) {num=1;}
       }
       return num;
    // 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(".png")-1;     // start as far right in string as possible
    //      this should avoid any URL garbage from mobile phones.
       var ISnam=Pnam.substr(NamLoc,1);      // page number only
       var NUpg="images/CodatronP";          // first part of desired result
    //      then tack on new page number and image filetype
     if (updn ==="up") {
        pg=incrPage(1,ISnam);
        document.getElementById("imgpage").src = NUpg+pg+".png";
      }
      else {
        pg=incrPage(-1,ISnam);
        document.getElementById("imgpage").src = NUpg+pg+".png";
      }
    }
    It would be nice to write some code that would "send" that mobile phone URL to somewhere else for me to read, eg: one of my FTP sites.

  2. #17
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by robertbaer View Post
    Code:
    var num=+nampg.valueOf();
    I would remove the valueOf method invocation here. It serves no purpose.

    Quote Originally Posted by robertbaer View Post
    Code:
    if (dir===-1)
       {
       num=num-1;
       if (num===0) {num=7;}
       }
      else
       {
       num=num+1;
       if (num===8) {num=1;}
       }
       return num;
    // Sequence is 1,2,,7,1 for plus, 1,7,6,,1 for minus
    }
    You can use shortcut operators to add to a variable. Example:

    Code:
    num -= 1;
    num += 1;
    Quote Originally Posted by robertbaer View Post
    Code:
       var NamLoc=Pnam.search(".png")-1;     // start as far right in string as possible
    This doesn't do what you think it does. Your code will match abcpng, for example.

    Try this following example with three uses of the alert method. The first and second uses of alert demonstrate that your argument matches invalid strings by showing your argument followed by the correct argument. The first use uses type-converted strings as arguments. The second use uses regular expressions.

    The third use of alert shows how both arguments match a correct string, thereby masking this error until incorrect input is used.

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    		<script>
    			var string = "abcpng";
    			alert(".png in abcpng: " + string.search(".png") + "\r\n\\\\.png in abcpng: " + string.search("\\.png"));
    			// Better code that uses arguments of the proper type:
    			alert("/.png/ in abcpng: " + string.search(/.png/) + "\r\n/\\.png/ in abcpng: " + string.search(/\.png/));
    			string = "abc.png";
    			alert("/.png/ in abcpng: " + string.search(/.png/) + "\r\n/\\.png/ in abcpng: " + string.search(/\.png/));
    		</script>
    	</head>	
    	<body></body>
    </html>
    The search method takes a regular expression as an argument. If you use a string as an argument (as you've done here), then the string is converted to a regular expression. Using a string is actually more complicated though since you need to double-escape backslashes so that the backslash is interpreted as part of the regular expression and not as an escape character.

    Quote Originally Posted by robertbaer View Post
    Code:
       var ISnam=Pnam.substr(NamLoc,1);      // page number only
    As mentioned before, substr is a non-standard method. substring is defined in the ECMAScript (JavaScript) specification. I would use the standardized method:

    Code:
    var ISname = Pnam.substring(NamLoc, NamLoc + 1);
    It works slightly differently. substr takes two arguments: the start position of the substring and the substring length. substring, on the other hand, takes takes two substring positions with the first argument being the start position and the second being the end position.

    Also previously mentioned, you can use the charAt method since you just want one character: var ISname = Pnam.charAt(NamLoc);.

    Quote Originally Posted by robertbaer View Post
    It would be nice to write some code that would "send" that mobile phone URL to somewhere else for me to read, eg: one of my FTP sites.
    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    		<script>
    			if (true) {
    				document.location.replace("http://www.example.org/");
    			}
    		</script>
    	</head>	
    	<body></body>
    </html>
    You can also use the apply method if you don't consider the mobile page just another representation of the same content. That'll leave the current page in the browser's navigation history though.
    Last edited by Arbitrator; 09-03-2013 at 12:50 PM. Reason: I re-mentioned the charAt method.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  3. #18
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    1) In your 3-alert example, i noted exactly what i expected; no match on the first two which is correct, and a match on the last.
    I think you may be alluding to my assumption that the GET will return, at minimum, the "images/CodatronP4.png" (where the page number is from 1 to 7) AND that (perhaps) may be incorrect.
    I look at it this way, in the HTML document, that string is exactly what is "in" src so the possibility is essentially zero.
    And if by some remote chance and SNAFU of a mobile browser, it gets trashed, then the whole page at that point becomes non-recoverable, a user cannot be assumed to have any intelligence.
    **
    Thanks to you, i now know three ways to get a cat out of its skin
    Using substring() makes it read better,but why in heck was the syntax of JS made to be so non-conforming to standards set by the first real languages like Cobol, Fortran and Algol?
    At least charAt() is not messy and looks good as well.
    *
    Oh my! That last example is SOOooooo _SIMPLE_ and neat and ...
    Much thanks!

  4. #19
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by robertbaer View Post
    1) In your 3-alert example, i noted exactly what i expected; no match on the first two which is correct, and a match on the last.
    The period/full stop character matches *any* character and therefore invalid strings like "stopngo.html" match. That's what I was pointing out.

    Your ".png" string argument is type-converted to a regular expression argument since the latter is the kind of argument the search method requires. In regular expression syntax, a period/full stop character is equivalent to any character.

    The pattern was:
    • Alert 1: Your Reg Ex: Matches Invalid () / Fixed Reg Ex: Doesn't Match Invalid ()
    • Alert 2: Your Reg Ex: Matches Invalid () / Fixed Reg Ex: Doesn't Match Invalid ()
    • Alert 3: Your Reg Ex: Matches Valid () / Fixed Reg Ex: Matches Valid ()

    Quote Originally Posted by robertbaer View Post
    Using substring() makes it read better,but why in heck was the syntax of JS made to be so non-conforming to standards set by the first real languages like Cobol, Fortran and Algol?
    JavaScript is a mess because it's a Web language that was developed by competing browser vendors and standardized after the fact.

    It's also designed differently. There are no classes, for example. (IIRC, there was a proposal to add them in ECMAScript (JavaScript) 4, but Microsoft shot down that proposal, so now it only exists as ActionScript 3 (used in Adobe Flash Professional)).

    Not only that, but we're stuck with non-standard/obsolete/inefficient parts of the language for backward-compatibility reasons. Newbies, while learning, copy and paste bad code, which still works because it *has* to be supported and end up in forums like this one where people like me get to point out those issues ad nauseum.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  5. #20
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Am confused by all of what seem to be escape characters and the seemingly random use of more than one of them "/" and "\"; if there was only one escape character, it might make more sense.
    Worse, this funnybusiness of having undisclosed functions for some random character "." and then using a different character as the escape character is strange to say the least.
    I say undisclosed because i have not seen mention of which characters are "special" and what they exactly stand for.
    One could make "." very strict, in that the code parser would ignore everything at that point (the period) and all afterwards on that line
    One could use a different character at the end of a physical line to indicate that there is more code on the next line, implying code rattles on until there is blankspace at the end of a line.
    *
    Of the three corrected forms, i prefer the first over the second because it "looks" nicer.
    Your showing my ".png" string argument being type-converted to a regular expression is useful.
    But how in the heck does a newbie/dummy know that a given expression like ".png" is not what is considered a "regular expression"?
    I do not see any tutorials that cover what may be considered fundamentals.
    **
    If Javascript, and other variants are so messed up, how about use of a regular, reasonable language and toss them?
    To heck with M$, just because they act like a big bully does not mean one can be dictated by their whims.
    Say you want a better,more regular javascript that they do not want,then put support scripts on the web for free; seems any browser (even the worst, Safari, or the ugly, Opera) supports link= to external HTML code.
    That code can be the full new parser doing everything you want - and no more.
    Call it M$Grumble, anyone can still use javascript,and to use M$Grumble the syntax to start that code is <script M$Grumble> and can end with the standard </script>.
    *
    Blow their socks off!

  6. #21
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    Quote Originally Posted by robertbaer View Post
    Am confused by all of what seem to be escape characters and the seemingly random use of more than one of them "/" and "\"; if there was only one escape character, it might make more sense.
    Worse, this funnybusiness of having undisclosed functions for some random character "." and then using a different character as the escape character is strange to say the least.
    ...
    If Javascript, and other variants are so messed up, how about use of a regular, reasonable language and toss them?
    To heck with M$, just because they act like a big bully does not mean one can be dictated by their whims.
    Say you want a better,more regular javascript that they do not want,then put support scripts on the web for free; seems any browser (even the worst, Safari, or the ugly, Opera) supports link= to external HTML code.
    Blow their socks off!

    1. regexp is "a mess" in any language. in fact, JS's RegExp literals are much nicer to read than PHP's string-based implementation. you can blame perl if you don't like JS's implementation of RegExp.

    2. you can in fact use coffescript to kill off one level of quotes and have it run in all browsers.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  7. #22
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    What follows is core that i have used and investigated as far as i can without using editors on a cellpone:
    Code:
    function DM_redirect(MobileURL){
         // *TESTED*
     try {
    // avoid loops within mobile site
      if(document.getElementById("dmRoot") !== null)
      {
       document.writeln("dmRoot not null|");
       return;
      }
      var CurrentUrl = location.href;
    // yes; current URL now in variable
      var noredirect = document.location.search;
    // variable acts empty
            var NavAgent=navigator.userAgent;
    // index is -1 .. so "always" tests agent in match substrings
      var amatch = "";
      var TypesToMatch=/(iPhone|iPod|BlackBerry|Android|Mobile|webOS|Windows CE|IEMobile|Opera|HTC|LG-|LGE|Samsung|SEC-SGH|Symbian|Nokia|PlayStation|Nintendo DSi)/i
    // original long code---v
    // amatch = (navigator.userAgent.match(/(iPhone|iPod|BlackBerry|Android.*Mobile|BB10.*Mobile|webOS|Windows CE|IEMobile|Opera Mini|Opera Mobi|HTC|LG-|LGE|SAMSUNG|Samsung|SEC-SGH|Symbian|Nokia|PlayStation|PLAYSTATION|Nintendo DSi)/i));
       //     NavAgent="SaMsUnG here will match";  // gives amatch of SaMsUnG,SaMsUnG
    amatch = (NavAgent.match(TypesToMatch));
    // either amatch is null or is a string like "AbCd,AbCd" if AbCd was in either string
    //   regardles of case in other string.
    
      if (noredirect.indexOf("no_redirect=true") < 0)
       {
        if (amatch)
        {
         location.replace(MobileURL + "?url=" + encodeURIComponent(CurrentUrl));
        }
       }
      }
     catch(err){}
    }
    
    // Now, if any of those mobile phones used, use my new code
     DM_redirect("http://www.oil4lessllc.com/mindex.html");
    My problem is that in practice, on a real cellphone (Andrioid) the re-direct does not work.
    As a result, i did more extensive investigation of the match() as used in the code, and found that either amatch is null or is a string like "AbCd,AbCd" (ie: duplicated).
    Looks weird to me,but adding "mozilla" to the list allowed me to check it out online and offline.
    So..
    My conclusion is that even tho the cellphone is android "powered" its internal ID as reported back by navigator.userAgent does not have that character string in the contents.
    Therefore, i cannot trust any of those provided designators as being correct for any of the implied cellphone IDs.
    This is why i need a way to "send back" the IDs to somewhere i can look.
    The document.location.replace() only "jumps" to the given location.
    I do not know if there is a reliable way (via some script) to send a pre-calculated message from the cellphone as an e-mail, as simulating of texting appears to be not useful.

    Any ideas?

  8. #23
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by robertbaer View Post
    My problem is that in practice, on a real cellphone (Andrioid) the re-direct does not work.
    As a result, i did more extensive investigation of the match() as used in the code, and found that either amatch is null or is a string like "AbCd,AbCd" (ie: duplicated).
    Looks weird to me,but adding "mozilla" to the list allowed me to check it out online and offline.
    So..
    My conclusion is that even tho the cellphone is android "powered" its internal ID as reported back by navigator.userAgent does not have that character string in the contents.
    Therefore, i cannot trust any of those provided designators as being correct for any of the implied cellphone IDs.
    Right. User agent (UA) strings are inherently unreliable because user agents lie and have been doing so for a very long time now. And for those strings where the UA doesn't lie, the strings tend to be long and complicated. This is more true for cellphones in my limited experience. (Someone asked me for the string of my DROID RAZR MAXX and it was ridiculously long.)

    You're better off using CSS media queries, which take into account properties of the device such as screen width when determining how to handle requests rather than trying to account for thousands of cell phone models plus mobile video game platforms.

    Quote Originally Posted by robertbaer View Post
    This is why i need a way to "send back" the IDs to somewhere i can look.
    Use an existing website stat tracker. I can't think of one off the top of my head, but I know there are some that track the strings of the UAs that visit a site. You can find collections of the strings on websites too, but who wants to write a script to reliably process thousands of different strings?

    Quote Originally Posted by robertbaer View Post
    I do not know if there is a reliable way (via some script) to send a pre-calculated message from the cellphone as an e-mail, as simulating of texting appears to be not useful.
    You'd be looking at writing a PHP script. I tried to use the mail function and had no luck with it though.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  9. #24
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    the word segment "ndroid" should be in navigator.userAgent for any android device. i don't worry about people breaking their browser by choosing a wrong userAgent; that's on them, not me, and it's got to be no more than a percent or two of users. I doubt most folks even know what a "userAgent" is...

    i think devs spend way to much time fretting over people who've jacked up their browser in some way; disabling javascript, changing userAgents, and blocking prompt() are the most common examples. Sure, we know how and why one might alter something, but the vast majority of folks have little to no idea what these technologies/jargons even are, much less why one might alter the behavior of the browser to reflect a dev's aims and understandings.


    while it does seem silly to cordon off just android users for some reason, i cannot say the reason is invalid, and i wouldn't be too concerned about a tiny fraction of folks who might get excluded from or bundled with this group, whatever it's purpose is.
    Last edited by rnd me; 09-07-2013 at 07:38 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  10. #25
    New Coder
    Join Date
    Aug 2013
    Posts
    27
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I found that match() works with partial strings as well,but never mind; navigator.userAgent is completely unreliable as a tool to detect a webphone.
    There are about 147 different webphone browsers (count each and every version), and at least 2438 unique make/models of them.

    In discovering that,i gave up that approach and made it simple as all heck; I use navigator.userAgent to see if a standard browser is being used like any IE or mozilla and treat all else (esp Safari and Opera; i include Chrome as well) as WebPhone.

    PHP is out,and e-mail cannot be used because a given browser and/or OS may not support it; besides one must be registered with a provider for that (as well as for NGs).

    So i do the KISS and sleep well.
    Thanks everyone for the comments and support!


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •