-1

I had this problem 2 months ago as well, and yet I haven't been able to solve it.

My webpage has boxes where you can buy virtual money to my website, the pricing works like wonder with JavaScript (If I set 500 points $1, it will calculate all the prices automatically). Now whenever I click "Order", it would write something like this:

file:///C:/Users/MyName/Desktop/MyPage/transaction.html?totalprice=3

so the totalprice is set like this:

<a href="transaction.html?totalprice=6"><img src="" alt=""/></a>

I have tried over 10 different methods to print the "totalprice" to the transaction.html page, but it will not display it and using alert, it will say that it is undefined. What is wrong? Would it matter because I have not hosted the website, as it is in my browser only from a file.

How can I get query string values in JavaScript? I've tried every method from this area and none of them works for me.

Timppa
  • 353
  • 2
  • 7
  • 24
  • Do you see a JS error? – LiverpoolOwen Mar 27 '17 at 12:29
  • https://gist.github.com/marmeladze/f693501707940b1ea1af433c327800a9 – marmeladze Mar 27 '17 at 12:31
  • Post your code.. – Pugazh Mar 27 '17 at 12:31
  • Added pastebin! – Timppa Mar 27 '17 at 12:43
  • it works actually - https://i.stack.imgur.com/HZogy.png – marmeladze Mar 27 '17 at 12:45
  • Strange, for me it is blank, the part where the price should be displayed at. – Timppa Mar 27 '17 at 12:46
  • Can we see the full HTML ? –  Mar 27 '17 at 12:59
  • Romain it would be pretty difficult as this is like thousand lines of code (but basicly repeptive) and it is in different language than English so I would have to change everything to English, as I already changed the variable names and descriptions. I will post the transaction.html page where it should display the code.

    You're about to buy 3000 PTS for

    – Timppa Mar 27 '17 at 13:03
  • OH and LiverpoolCoder, I see no error at all. Alerting will just say that it's undefined. – Timppa Mar 27 '17 at 13:16
  • Please read [How does accepting an answer work?](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work). Don't edit the word "solved" into your question. – Quentin Mar 27 '17 at 13:43
  • "this site hates me and won't let me display my code" — Try reading the formatting instructions next to the question entry box. Also examine the tooltips on the menu bar above it. You could also try reading the help pages. – Quentin Mar 27 '17 at 13:45

1 Answers1

0

Follow this.

     function getParameterByName(name, url) {
         if (!url) url = window.location.href;
         name = name.replace(/[\[\]]/g, "\\$&");
         var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
             results = regex.exec(url);
         if (!results) return null;
         if (!results[2]) return '';
         return decodeURIComponent(results[2].replace(/\+/g, " "));
     }

      window.onload = function() {
        document.getElementById("p_1").innerHTML = getParameterByName('totalprice', 'transaction.html?totalprice=1');
        document.getElementById("p_2").innerHTML = getParameterByName('totalprice', 'transaction.html?totalprice=2');
        document.getElementById("p_3").innerHTML = getParameterByName('totalprice', 'transaction.html?totalprice=3');
        document.getElementById("p_4").innerHTML = getParameterByName('totalprice', 'transaction.html?totalprice=4');
      }
<p>You are about to buy 500 pts for $<span id="p_1"></span></p>
<p>You are about to buy 1000 pts for $<span id="p_2"></span></p>
<p>You are about to buy 1500 pts for $<span id="p_3"></span></p>
<p>You are about to buy 2000 pts for $<span id="p_4"></span></p> 
marmeladze
  • 6,468
  • 3
  • 24
  • 45
  • Thank you for your help! Sadly I just solved this by myself after one example worked and I am very embarrassed that I totally forgot to use document.write()... This is why it never worked... (Sigh I hate front-end) >_ – Timppa Mar 27 '17 at 13:54