1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need to replace ; allcharacters

Discussion in 'JavaScript' started by qwikad.com, Sep 4, 2015.

  1. #1
    Need to replace ; allcharacters using:

    title = title.replace(//g, '');

    1. There's a space after ;
    2. By allcharacters I mean A-Z a-z 0-9 _

     
    qwikad.com, Sep 4, 2015 IP
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #2
    Actually after I posted this I tried title = title.replace(/\; .*/, ''); and it seems to be working fine. Is there a better way of doing this? It's tripping all characters (which I am fine with).
     
    qwikad.com, Sep 4, 2015 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Never seen anyone trying to get rid of all characters and semi-colons before... I'd be VERY interested in the usage scenario of this since that's a pretty unique situation. Are you pulling this to do something like count how many whitespace characters are present? Checking for UTF-8? I mean, if this is a TITLE I'd think you'd want to strip everything EXCEPT those characters, or reduce all whitespace to a single space.

    If you want the normal "safe alpha' characters AND the semi-colon, that would be:
    title = title.replace(/[\w;]/g, '');
    Code (markup):
    The code you presented should in fact not be matching ANYTHING, your search set is empty. (unless the forum stripped it?)

    Oops, I see, you only want semi-colons with spaces after them, not just semi-colons. (that's a REALLY weird filter)

    title = title.replace(/\w|; /g, '');
    Code (markup):
    Should do the trick, needed an "or" in there.
     
    deathshadow, Sep 5, 2015 IP
  4. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #4
    qwikad.com, Sep 5, 2015 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    Yeah, it is weird. It's the thing I found online that displays links / titles of recently visited ads and somehow it was also "catching" other stuff from the site:

    ; cl_cityid or ; PHPSESSID or ; lang&cityid etc. etc. after the titles. Not sure why it was doing that. I am using title = title.replace(/\; .*/, ''); and it's stripping it fine, but I figure it's probably not the best way of doing it.
     
    Last edited: Sep 5, 2015
    qwikad.com, Sep 5, 2015 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    That PHPSESSID sounds more like it's returning the entire HTTP response headers since that's a cookie name. Yeah, that's what that is, it's returning the cookie string since those are "; " delimited.

    You willing to link to said script and/or provide some sample data? I've got the feeling that what should be done there is a split to separate out all the "extra' data being returned instead of a regex -- either that or it's method of parsing the cookie string from the header to try and extract a title is flawed.
     
    deathshadow, Sep 5, 2015 IP
  7. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #7
    It's by this guy:

    http://www.willmaster.com/library/features/visitor-page-view-history-breadcrumbs.php


    <script type="text/javascript"><!--
    if(HistoryContent.length) {
    
    document.write('<'+'div ');
    document.write(' id="visitorhistory" ');
    document.write(' style="border-style:solid; border-width:1px; padding:0 5px 10px 20px;">');
    
    document.write('<'+'h3>You were here:<'+'/h3>');
    
    document.write(HistoryContent);
    
    document.write('<'+'h3>You are here:<'+'/h3>');
    
    document.write(CurrentPage);
    
    document.write('<'+'/div>');
    
    }
    //--></script>
    
    Code (markup):
    <style type="text/css">
    div#visitorhistory p {
       font-weight:bold;
       margin-top:3px;
       margin-bottom:3px;
       }
    div#visitorhistory a {
       font-style:italic;
       text-decoration:none;
       }
    </style>
    
    Code (markup):
    <script type="text/javascript"><!--
    // Obtained from http://www.willmaster.com/
    //
    // Specify maximum number of history links to keep,
    //    minimum 1.
    var MaximumNumberOfLinks = 15;
    
    // Specify cookie name.
    var CookieName = "HistoryLinks";
    
    // Specify number of days cookie is to remain on visitor's
    //    computer. (Use 0 to delete cookie when browser closed.)
    var DaysToLive = 366;
    
    // No other customizations required.
    var HistoryLink = new Array();
    var HistoryTitle = new Array();
    var CurrentPage = new String();
    var HistoryContent = new String();
    DaysToLive = parseInt(DaysToLive);
    MaximumNumberOfLinks = parseInt(MaximumNumberOfLinks);
    if( MaximumNumberOfLinks < 1 ) { MaximumNumberOfLinks = 10; }
    
    function GetCookie() {
    var cookiecontent = '';
    if(document.cookie.length > 0) {
       var cookiename = CookieName + '=';
       var cookiebegin = document.cookie.indexOf(cookiename);
       if(cookiebegin > -1) {
          cookiebegin += cookiename.length;
          var cookieend = document.cookie.indexOf(";",cookiebegin);
          if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
          cookiecontent = document.cookie.substr(cookiebegin,cookieend);
          }
       }
    if( cookiecontent.length < 3 ) { return; }
    cookiecontent = unescape(cookiecontent);
    var historyList = cookiecontent.split('&');
    for( var i = 0; i < historyList.length; i++ ) {
       var link = historyList[i].split('=',2);
       HistoryLink.push(link[0]);
       HistoryTitle.push(link[1]);
       var temparray = link[0].split('~amp;');
       link[0] = temparray.join('&');
       temparray = link[1].split('~amp;');
       link[1] = temparray.join('&');
       HistoryContent += '<'+'p>'+'<'+'a href="'+link[0]+'">'+link[1]+'<'+'/'+'a>'+'<'+'/'+'p>';
       }
    }
    
    function PutCookie() {
    if( HistoryLink.length < 1 ) { return; }
    var len = HistoryLink.length;
    while( HistoryLink.length > MaximumNumberOfLinks ) {
       HistoryTitle.shift();
       HistoryLink.shift();
       }
    var pairs = new Array();
    for( var i = 0; i < HistoryLink.length; i++ ) { pairs.push(HistoryLink[i]+'='+HistoryTitle[i]); }
    var value = pairs.join('&');
    var exp = new String();
    if(DaysToLive > 0) {
       var now = new Date();
       now.setTime( now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000) );
       exp = '; expires=' + now.toGMTString();
       }
    document.cookie = CookieName + "=" + escape(value) + '; path=/' + exp;
    }
    
    function RecordCurrentPage() {
    var link = document.URL;
    var title = document.title.length > 1 ? document.title : 'Untitled';
    CurrentPage = '<'+'p>'+title+'<'+'/'+'p>';
    var temparray = link.split('&');
    link = temparray.join('~amp;');
    var temparray = title.split('&');
    title = temparray.join('~amp;');
    HistoryLink.push(link);
    HistoryTitle.push(title);
    }
    
    GetCookie();
    RecordCurrentPage();
    PutCookie();
    //--></script>
    
    Code (markup):
     
    qwikad.com, Sep 5, 2015 IP
  8. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #8
    Oh great Pesci, whiskey tango foxtrot.

    See this is what happens when people NOT QUALIFIED TO WRITE JAVASCRIPT, go and share the sloppy garbage scripts they wrote. I'm not sure what's worse; the bloated empty declarations, the multiple VAR for nothing, dumping crap into the global namespace, or the pointless retard comments. REALLY? "CookieName" is the cookie name? I'd NEVER have guessed. :( Doesn't even follow JS naming conventions properly. (not that JS itself does all that great a job of that)

    ... and that's before we even talk the sample output script's multiple document.write forcing multiple reflows. (there's a REASON document.write REALLY needs to go the way of the dodo)

    ... and I don't even want to THINK about the train wreck of bloat that massive cookie is going to add to every blasted file request. Really what that's doing is another of those things I would never EVER, EVER do on a website. That could EASILY push the cookies past the 8k limit that Apache sets.

    Now that said, if I wanted to increase both server load and decrease page load times with that type of asshattery...

    The biggest functionality problem is that code isn't using encodeURIComponent when storing the values and decodeURIComponent when recovering them SEPARATE from the escape/unescape done for the cookies. (which should ALSO be changed to same). It's detecting the ampersands found in URI's that have them as being the delimiters between components. Those need to be changed to %xx before storing the URI (or title for that matter), then the % need to be changed to %xx before storing as the cookie. It's missing that extra step. It sucks to encode and encode, but that's how it should be done.

    Gimme a few and I'll toss together something a bit better for you on that. Will be similar, but not entirely the same in calling conventions. First thing I'll probably do is separate out the cookie handling and use my docCookie object, that's based on some code I wrote 15 years ago that I think Adobe stole from me and bloated the crap out of. The similarities are eerie, they basically added a bunch of parenthesis for nothing and changed "start" and "stop" to "begin" and "end" :(

    Hardcoding the cookie handling into the object, creating all those internal functions for NOTHING apart from code bloat, and then calling the functions? Needlessly pointlessly convoluted. Probably wouldn't be a bad idea to add timestamp functionality as well so you have some idea WHEN they were tracked...

    But that's more data -- and therein more data to run up against the 8k cookie size limit; there's a reason this type of thing is better tracked server-side in a database with reporting handled by AJAX or sessions.

    Lemme see what I can come up with.
     
    deathshadow, Sep 6, 2015 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #9
    alright, here's what I came up with:
    http://www.cutcodedown.com/for_others/qwikad/history/

    I did NOT put live copies of the markup or js on my site as I don't want those massive cookies anywhere NEAR my server. Properly named .js and .html files can be found in the .rar file in that directory.

    the script itself (follow along with the .txt):
    http://www.cutcodedown.com/for_others/qwikad/history/history.js.txt

    Starts out with my stock cookie functions, and my make/selector handler so the test page can be built on the DOM instead of with document.write. Might seem like more work but it's WAY faster than forcing a reflow every blasted time a .write ends. Much less buggy reflows when the markup is incomplete.

    Instead of making separate arrays for title and url, I have a single array as a property of historyRecord called 'history'. This array contains an object with the structure { title, url, time }. The time index of the access is recorded as a unix timestamp.

    The historyRecord.start function must be called from the calling program to work. It accepts the maximum number of links, cookie name, and expiration time. I set the defaults to 10, 'HISTORYLINKS' and 366.

    First thing I do in the routine is make sure cookiename is set, if not I set it. Then I grab the cookie and create a copy of the current time, and set up the expiration date. I then empty history AGAIN so that if desired you can call this routine to pull from the database more than once. You could do this to change the cookie on the fly or even the expiration time.

    I stored the cookie in the TEMP variable so we can detect if it exists or not, since we'd only want to populate history[] if it does. I split by ampersand, then loop through that split copy reusing that same temp variable so as to not waste time allocating a new one. The parts are then split by equals signs, then pushed as an object onto history[] AFTER doing a decodeURIComponent on it.

    After history is checked and/or populated I then check if maxlinks is set, if not set it. I then check if the history length >= maxlinks and if it is longer, I slice off just the end of the array. (WAY faster and easier than the iterating shift).

    I then push the current page onto history[], including the current timestamp.

    To then set the new version of the cookie we turn our temp variable into an array, then fill it with our title, url and time making sure we encodeURIComponent them FIRST. (technically we shouldn't have to do it with time, I say better safe than sorry. You know me, safety forced.). Once they're all nicely pushed onto the array and encoded we can just set the cookie using join to slap those ampersands between them.

    In the markup / test code:
    http://www.cutcodedown.com/for_others/qwikad/history/template.txt

    Pretty standard document. I like to load my scripts right before </body> so that's where I did that. We include history.js then call historyRecord.start(15) to set it to 15 instead of ten just to test that functionality. I then use the DOM to build a table to show the complete record in a digestable format using the "make" method I included in my little set of utilities. (Even I admit that document.createElement inhales upon the proverbial equine of short stature).

    I "make" the table, creating the caption at the same time as it's primary child. I then add the thead, tfoot and tbody, and create a temp variable to hold our TR. REMEMBER, tfoot goes BEFORE tbody, as nonsensical as that seems. (It's a print thing). Finally I create a time element since trying to use time as a local var can be... unpredictable in some browsers (Opera, chrome) meaning if we did new Date(timestamp) every time, we'd get undefined as the result on all but the last one. (damned if I know why).

    I then add the proper headings to our thead with the proper scope, adding a class to the URL column so we can adjust that's alignment. (with varying length URL's it just looks better left justified)

    From there we use temp as a TR in TFOOT to create some information, to which we re-use temp again as the TD|colspan=4 with a span inside it to show our cookie size. I figured that might be a good number to monitor since again, those massive cookies could really bite you in the backside FAST. I then append the number of records just to be sure everything adds up. If we align left, we can float the span right to put them on each side of the tfoot.

    All that's left is to iterate through historyRecord.history. Pretty simple loop we make a new TR in temp, we adjust the time (again we can't do new Date() here on a local var due to oddball behaviors. JS date handling sucks), then add our TH with appropriate scope and three data records.

    I went with a table because with this much information it really does feel like tabular data.

    One nice side effect of using the DOM, is we don't have to worry about someone sneaking markup into this on us. IT HAPPENS, trust me on this. Someone slides a little encoded markup into some getdata and then you dump it out with innerHTML or document.write, it can bite you in the backside REAL fast with things like XSS attacks.

    I kept the CSS pretty simple, fanciest thing is just using nth-child to colour every other row.

    So... that's how I'd do it that way -- NOT that I'd do it that way. I'd be sending the new URI to a server via AJAX for storage in a proper database. Again, the massive cookie is just... ouch. NOT a good idea. It's stuff like this that gives cookies a bad name.

    Well, that or I'd just record it when the user made the request in the first place completely transparently... which would make WAY more sense.

    In any case, you can DL the full thing in that .rar:
    http://www.cutcodedown.com/for_others/qwikad/history/qwikad.rar

    Hope this helps. It's bad practice how it's doing it, but what was breaking it was a failure to have that second layer of encoding before adding the & and = between values.
     
    deathshadow, Sep 6, 2015 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Just UL'd minor update, minor bugfix in ancient code -- let's just say I don't play with cookies from JS very often anymore.
     
    deathshadow, Sep 6, 2015 IP