0
<script type="text/javascript">
$(function(){
  $("a[rel='tab']").click(function(e){
    e.preventDefault();

    pageurl = $(this).attr('href');

    $.ajax({url:pageurl+'?rel=tab',success: function(data){
      $('#right_column').html(data);
    }});

    if(pageurl!=window.location){
      window.history.pushState({path:pageurl},'',pageurl);
    }

    return false;
  });
});
</script>

The previous code is what I am using to update a div element with content when a link is clicked with rel="tab" inside the <a> tag.

Example <a> Tag:

<a href="index.php?v1=variable1&v2=variable2" rel="tab">Click Here</a>

So now, I need to update the following lines of the javascript code above to substring out index.php out of the link...

 $.ajax({url:pageurl+'?rel=tab',success: function(data){
          $('#right_column').html(data);
        }});

But need to keep the full url for the last section of the javascript as pageurl.

How would I do something like this?

I did research on this and tried implementing the answer listed here: How to substring in jquery but couldn't seem to get it to work.

Community
  • 1
  • 1
kdjernigan
  • 417
  • 2
  • 7
  • 22
  • 1
    Try `pageurl.replace('index.php', '')` instead. EDIT: Actually, I'm confused as to whether you want to keep or remove index.php. – Popnoodles Dec 24 '13 at 02:54
  • So you just want `pageurl` to be `?v1=variable1&v2=variable2&rel=tab`? – Phil Dec 24 '13 at 02:54
  • popnoodles - I tried, it didn't appear to work. Maybe I implemented it wrong. Can you implement it into the code so I can find out if I did it correctly? – kdjernigan Dec 24 '13 at 02:55
  • Phil - only for `$.ajax({url:pageurl+'?rel=tab',success: function(data){ $('#right_column').html(data); }});` ... I need `if(pageurl!=window.location){ window.history.pushState({path:pageurl},'',pageurl); }` to remain the same with index.php included. So a new variable will be required. – kdjernigan Dec 24 '13 at 02:56

3 Answers3

2

It looks like you want to do this, though I'm not sure why.

$.ajax({url:pageurl.replace('index.php', '') +'&rel=tab' ...

Note the & not ? on the concat, as the URL already has ?

Popnoodles
  • 28,090
  • 2
  • 45
  • 53
  • I am using this code to do 2 things. #1) Update URL with new variables without reloading page... #2) Update index.php to view.php (needed it to be a different page) for the content loaded into the right column so that it retrieves the new variables every time a link is clicked and variables change. I am retrieving the variables through php `$_GET` and including different pages into that column based on what the variables are. I am doing this so that my left column doesn't have to reload because its a jquery based navigation and it does animation whenever it is loaded. – kdjernigan Dec 24 '13 at 03:10
  • 1
    So `.replace('index.php', 'view.php')` might be better – Popnoodles Dec 24 '13 at 03:24
  • yeah that is what I ended up changing it to. Thanks for your help :) I've posted another question about this code as another SO question. It is involving the back button not working with an additional piece of code not included in this question. The link is at http://stackoverflow.com/questions/20754629/javascript-jquery-back-button-not-changing-content-at-all – kdjernigan Dec 24 '13 at 03:54
1

I think you want the URL without the query string.

$.ajax({
    url: pageurl.split('?')[0]+'?rel=tab',
    success: function(data) { $('#right_column').html(data); }
});

This splits the string on the question mark, which creates an array. The first element of the array is the part of the string before the question mark. It will be the entire string if there is no question mark.

John S
  • 21,212
  • 8
  • 46
  • 56
  • This would have worked, except I ended up not being able to use '' there. I had to change index.php to view.php as it turned out as I was testing the code so `$.ajax({url:pageurl.replace('index.php', 'view.php') +'&rel=tab'` ended up being a better solution for the code. Thanks for your contribution though :) – kdjernigan Dec 24 '13 at 03:12
0

If you just want the query string from the href attribute, try this

var ajaxUrl = this.search + (~this.search.indexOf('?') ? '&' : '?') + 'rel=tab';
$.ajax({
    url: ajaxUrl,
    // ...
Phil
  • 157,677
  • 23
  • 242
  • 245