3

UPDATE

Sorry my qusetion was unclear i have updated my question

I want to add a query string in the end of url using anchor

For example if i have url

www.mysite.com/home

And have anchors on this page

 <a href='?page=1'>1</a>
 <a href = '?page=2'>2</a>
      .
      .
 <a href = '?page =x'>x</a>

When i click on these anchor link it directs me to Url

www.mysite.com/home?page=x

That what i want , I just want to add query string page in the end of the url,

But in the case of

www.mysite.com/home?category=sports

Or

www.mysite.com/home?category=music

Then if i click on the same anchor link

It redirects me to

www.mysite.com/home?page=x

But i want it to direct me to

www.mysite.com/home?category=sports&page=x

And now if i again on any of those link like

<a href='?page=1'>1</a>

Then i want it to direct me to

www.mysite.com/home?category=sports&page=1

is their any simple solution to do this , Or i have to use any complex javascript code.

thanks

4 Answers 4

1

It is simple.

var url ="www.mysite.com/home?page=1"; 
var result = url + (url.indexOf('?')== -1 ?  "?page=1" : "&page=1")
Sign up to request clarification or add additional context in comments.

Comments

0

window.location.replace(url);

url - is the string you want to open

Comments

0

You have to add all params to the link, like this:

<a href='?category=sports&page=1'>1</a>
<a href='?category=sports&page=2'>2</a>
.
.
.
<a href='?category=sports&page=x'>x</a>

2 Comments

their are many categories, Assume i have url www.mysite.com/home?category=music
What if the query parameters are dynamic?
0

I don't recommend using this but I think this is the only solution:

<a href="Javascript: if (window.location.href.indexOf('page=')==-1) { window.location=(window.location.href+ (window.location.href.toString().indexOf('?')>-1 ? '&page=1': '?page=1'))}">click here</a>

3 Comments

Hey challenger I updated my question , hope now u can understand what i am asking
mmm, I don't know how to solve this actually, check this link hope it can help : stackoverflow.com/questions/8764288/…
what is id here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.