-2

EDIT: Seems I describe my problem to complicated: My Question below is: WHY does my shorthand code only work when my input-variable contains '=' and NOT when it doesn't ?

I retrieve the variable with this code:

var hash = getParameterByName('find')

I have recently rebuilt the entire site and by that removed many pages which now results in 404-errors from old external links. I catch those 404-links and convert them into 301-redirects in order to maintain backward compability (Using web.config)

For example the 301-error-catch sends me this:

  www.sitename.com?find=find.aspx?f=something

Where as regular referal-links sends me this:

www.sitename.com?find=something

NOTE! the difference after '?' in the PARAMETERS sent to my page. I need to strip everything before the last '='-character.

NOTE! that the Parameter (from 301-error) contains the link to a non-existing page: "find.aspx?". IF the parameter-variable does NOT contain '=' - nothing is to be done.

my latest try is:

var hashtag = getParameterByName('find')
var findParam = hashtag.indexOf('=') ? findParam.split('=')[1] : getParameterByName('find');

i have also tried this:

var hashtag = getParameterByName('find')
var findParam = hashtag.indexOf('=') ? findParam.split('=')[1] : hashtag;

The split part of the shorthand code, actually works. IF hashtag.indexOf returns >0 it works, BUT if it returns -1 I get 'undefined' as result. How Come ?

I would think that by using the shortcode I would be able to clean up the search parameter quite nicely. But I cannot figure out how I go wrong here?

XanderMan
  • 137
  • 1
  • 15
  • is not query string is you want to grab and parse ? https://stackoverflow.com/questions/4656843/jquery-get-querystring-from-url or you just trying to split a string? 'cause i'll try to build you some script.People should not vote negative when the subject is confused or bad speaker of a specific language! Programmers are part of the solution not for a new problem !!!!! –  Jan 07 '18 at 09:34
  • Basically I just want to determine if the parameter sent to the page is from an old external link or not. Let's just say that if a forumpost redirects to our site using a non-existing link I will re-format that link. I simply look for url parameters containing the '=' as part of the string. This tells me it is from an old link. Parameters that does not contain that, should go through to the page without reformatting. does that clear things up ? – XanderMan Jan 07 '18 at 10:22
  • ok! i put you the possibility to run the script from RUN CODE SNIPPET –  Jan 07 '18 at 13:53
  • Given the posted URL: `www.sitename.com?find=find.aspx?f=something` what precise URL do you want to end up with? The explanation of '...remove everything from the string before the '='...` is ambiguous at best. If you could show (for example) four expected 'input' URLs and the expected output for each of those URLs that might help clarify your question. – David Thomas Jan 07 '18 at 14:00
  • 1
    @David Thomas yes it is! But if you pay attention he talk about PARAMETER of www.sitename.com?find=find.aspx?f=something so we suppose is the value find.aspx?f=something and he needs that find.aspx before that '=' inside that value of the first 'find' replaced 'cause maybe is expired! this I understand and i send him a version to replace everything he want not only that! –  Jan 07 '18 at 14:11
  • @Constantin: congratulations. But still: I want the OP to clarify what s/he actually wants before I start to make (inherently flawed) guesses and assumptions. – David Thomas Jan 07 '18 at 14:13
  • @David Thomas ah,ok ! "guesses and assumptions" is what you get in life as programmer.not even close what they talk about in manuals.Manipulating all infos you get and the target= solve a problem count to "a boss" or specific public interaction----feedback will define the kind of programmer you are.(true or not) "Precise" doesn't exist in programming ,i'm programming in several languages and from 1992' almost 12-18 hours minimum at day ... –  Jan 07 '18 at 14:20
  • The REAL question was, and still is:WHY does only half of the shorthandcode work? WHY will it only work when the input variable contains '=' and not when it doesn't ? – XanderMan Jan 08 '18 at 06:30

1 Answers1

0

The script i sent you is doing only parsing an old link with modifies you want=you set . if you uncomment the line where is whoweare var set then with small modifies instead the var str you can use 'whoweare'. not sure were aspx works are similar settings as apache's .htaccess .. in that case you can redirect using .htaccess to a script ,decide in that script is new or old post and then to output the right page. the simulate situation when a post is wrong is what i send you :

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  
  <title>link parser</title>
 </head>
 <body>
  <script type="text/javascript">
  <!--
  var str="www.sitename.com?find=find.aspx?f=something";
  
var mainwebsite="www.sitename.com";
 // replace parts what you want
var otherwebsite="www.ifiwant.com";
var rep0="variable";
var rep1="yes.php";//
var rep2="dsadas";
var rep3="varnew=other";
/*
//uncommenting this will allow you to had other settings:
// replace parts what you want
var otherwebsite="";
var rep0="";
var rep1="yes.php";//
var rep2="";
var rep3="";

*/

function isold(stx){
 //put here what you need to detect is old i put true to make work the script to parse modifies
 console.log('isold:'+stx);
 return true;
}

//https://stackoverflow.com:80/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
function loc(strx){
var loc = document.createElement("a");
 loc.href =strx;
/*
 loc.protocol; // => "https:"
 loc.host;     // => "stackoverflow.com:80"
 loc.hostname; // => "stackoverflow.com"
 loc.port;     // => "80"
 loc.pathname; // => "/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript"
 loc.hash;     // => "#hash"
 loc.search;   // => "?search=test"
 loc.origin;   // => "http://stackoverflow.com:80"
*/
 return loc;
}


//whoweare=loc(location);
      //console.log(whoweare.host);
//ref=loc(document.referrer);
      //console.log(ref.host);

mylocsearch=loc(str).search.substr(1);
      console.log(mylocsearch);

firsteq=mylocsearch.search('=');

 if(firsteq!=-1)
  {
      console.log(firsteq);
 st0=mylocsearch.substring(0,firsteq);
      console.log(st0);
 
 st1=mylocsearch.substring(firsteq+1);
      console.log(st1);
 st2=st1.substring(0,st1.indexOf('?'));
      console.log('!!!'+st2);
 if (!isold(st2)){
  location.href=str;//put here what you need if the part is not expired
 }
 oq=loc(st1);
 if(oq.search!='')
 {
      st3=oq.search.substr(1)
      console.log(st3);

      console.log(mainwebsite+'?'+st0+'='+st2+'?'+st3);//we are in control now we can replace everything 
                  //we need and we can replace all rep non ''
      console.log((true?'yes':'no')); //we use this and     mainwebsite+'?'+st0+'='+st2+'?'+st3
      finalurl=(otherwebsite!=''?otherwebsite:mainwebsite)+'?'+((rep0!='')?rep0:st0)+'='+((rep1!='')?rep1:st2)+'?'+((rep3!='')?rep3:st3);
      console.log(finalurl);
      document.write('<a href="'+finalurl+'">'+finalurl+'</a>');
 }

  
  }
  
  //-->
  </script>
  
 </body>
</html>
  • Thank you for that work. And thank you for standing up for me. But my question remains, why doesn't my shorthand code work? I am not in need of rewriting my URL only the parameters sent to it. – XanderMan Jan 08 '18 at 06:44