Title says it all. Basically I am looking to retrieve the end of the url using jQuery to add one to the selection and go to that page. I had some help from someone else on her but I want to understand how it is working/ why it's not working:
var urlFrags = window.location.href.replace(".html", "").split("_"),
curPage = parseInt(urlFrags[urlFrags.length]),
nextPage = "example_" + (curPage + 1) + ".html";
It could be an error somewhere else, but I am working down the line trying to bug-fix it.
I understand pretty much everything going on, except I don't understand parseInt() and stuff.
Would it be possible to '.getAsIntiger(urlFrags)' ? (I have tried this but I may have implemented wrong.)
The error there is here:
curPage = parseInt(urlFrags[urlFrags.length]);
You should be doing
curPage = parseInt(urlFrags[urlFrags.length-1]);
Since if you have a length of 2 in your array, you cannot access Array[2] because it doesn't exist. You have to access Array[1] because the array index starts in 0.
Why not do a var num = parseInt(/.+?_(\d+)\.html/.exec(window.location.href)[1])
Related
I'm creating a chrome extension that basically finds a string of text such as this (note the different numbers):
id%22%3A99986%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A1002938%2C%22name%22%3A%22null%22%7D%2C%7B%22
and then usese javascript to swap that text above with this:
id%22%3A77764%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A77984%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A87746%2C%22name%22%3A%22null%22%7D%2C%7B%22
I can't manage to make this work whatsoever. All I'm able to do is swap out the ID numbers and replace individual parts of the code whereas I want to improve it by replacing with larger pieces of code. Can someone help me get past this because I'm confused.
Here is the code that works for me:
document.body.innerHTML = document.body.innerHTML.replace(/99986/g, '77764');
What I'm trying to do is to replace one piece of code with two pieces of code (obviously wrong but it's clear what I'm trying to do):
document.body.innerHTML = document.body.innerHTML.replace(/id%22%3A99986%2C%22name%22%3A%22null%22%7D%2C%7B%22/g, 'id%22%3A77764%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A77984%2C%22name%22%3A%22null%22%7D%2C%7B%22');
Update 1:
Thank you Emeeus, your code worked great! Unfortunately I made an error in my example so I had to fix it up a bit from my end. This is the new code using your layout:
var strA =
"%7Bid%22%3A1001%2C%22name%22%3A%22The+Antique+Store%22%7D%2C%7B%22id%22%3A1010%2C%22name%22%3A%22Clothes%22%7D%2C%7B%22id%22%3A1349%2C%22name%22%3A%22Old+Store%22%7D";
var strB = "%7Bid%22%3A1001%2C%22name%22%3A%22The+Modern+Store%22%7D%2C%7B%22id%22%3A1010%2C%22name%22%3A%22Clothes%22%7D%2C%7B%22id%22%3A1349%2C%22name%22%3A%22New+Store%22%7D";
var arrA = JSON.parse(decodeURIComponent(',{""' + strA + '",:""}'));
var arrB = JSON.parse(decodeURIComponent(',{""' + strB + '",:""}'));
console.log(arrA)
console.log(arrB)
var res = Object.assign(arrA, arrB);
console.log(encodeURIComponent(JSON.stringify(res)))
But I'm met with this error "Error: Unexpected token , in JSON at position 0". Any ideas?
I think you should check if the typeof string you are trying to replace is string. I tested, check if that solves your problem.
See screenshot:
Your strings are parts of a JSON URI-encoded, so I suggest first to decode the strings an then parse them using JSON.parse, then you could work with objects literals, which is easier most of times, here an example:
var strA = "id%22%3A99986%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A1002938%2C%22name%22%3A%22null%22%7D%2C%7B%22";
var strB = "id%22%3A77764%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A77984%2C%22name%22%3A%22null%22%7D%2C%7B%22id%22%3A87746%2C%22name%22%3A%22null%22%7D%2C%7B%22";
var arrA = JSON.parse(decodeURIComponent('[{"' + strA + '":""}]'));
var arrB = JSON.parse(decodeURIComponent('[{"' + strB + '":""}]'));
console.log(arrA)
console.log(arrB)
var res = Object.assign(arrA, arrB);//<-- example
console.log(encodeURIComponent(JSON.stringify(res)))//<-- you could encode the result again
I am trying to replace the content of a div tag with a certain value every 50sec via polling and jQuery. And I would like to access the value of that updated div, so I can save a request to the backend.
My problem is that once the content has been replaced, the browser displays it correctly, however the HTML stays the same as the beginning. I'm afraid this is a rather basic question, but I'm really curious about this.
Here I prepared an example to illustrate the issue: http://jsfiddle.net/LJgN6/7/
And you can see it out ot JSfiddle's context to check the final HTML here: http://jsfiddle.net/LJgN6/7/show
I would like to achieve a way to have in the final HTML(i.e. right click, view page source):
num 1.1 replaced with num 1.2
num 2.1 replaced with num 2.2
...
The code that you see in the View Source window is the HTML that was sent to the browser before anything client side was allowed to modify the DOM. If you want to see the source as it is modified by your javascript, you need to inspect the DOM (IE F12, Firebug, etc.)
If you need to access this value that was inserted earlier, using javascript to access the contents of your element (ie. $('#number-2').text()) should return its contents as they are currently in the DOM.
EDIT: Corrected typo
It looks like you're already familiar with jQuery so I would go ahead and head over to the extremely helpful API and take a look at AJAX calls. There is plenty of documentation there that will help you.
http://api.jquery.com/jQuery.ajax/
Here's an idea. Take a look at my fiddle of your problem
$(document).ready(function(){
$('#number-1').html("num " + 1.1);
$('#number-2').html("num " + 2.2);
$('#number-3').html("num " + 3.3);
$('#number-4').html("num " + 4.4);
setInterval(function(){newInfo();}, 3000);
});
function newInfo(){
var firstDiv = $('#number-1');
var secDiv = $('#number-2');
var thdDiv = $('#number-3');
var frthDiv = $('#number-4');
var firstDivInfo = firstDiv.text().substr(4);
var secDivNewInfo = firstDiv.text().substr(4);
var thdDivNewInfo = secDiv.text().substr(4);
var frthDivNewInfo = thdDiv.text().substr(4);
var newNumber = (Math.random() + 1).toFixed(1);
secDiv.html("num " + secDivNewInfo);
thdDiv.html("num " + thdDivNewInfo);
frthDiv.html("num " + frthDivNewInfo);
firstDiv.html("num " + newNumber);
}
Hello I'm new to javascript and I need a little help with regex/replace
I want to take a url for example (url case 1)
http://url.com/_t_lastUpdate_2670619?location=50457347
or (url case 2)
http://url.com/_t_2670619
I want to select in this case just
_t_2680619
Ignore everything after "?" and before (underscore)t(underscore)
Is it possible with regex/replace? the only thing I managed to do was select numbers only with
var _t__and_number = document.URL.replace(/[^\d]/g, '');
alert(_t__and_number);
But that doesn't solve my problem if there's something like url case 1
(if I could get just the first number even without the (underscore)t(underscore) it would already help me a lot.
Thanks
Solutions:
onetrickpony/michaelb958:
var _t__and_number = window.location.pathname;
alert(_t__and_number);
ermagana:
var _t__and_number = document.URL.replace(/\?.*/g, '').match(/.*\/(.*)/)[1];
alert(_t__and_number);
As suggested by onetrickpony, if this is the URL you are currently browsing, window.location.pathname will yield that part of the URL.
First Part like case (1) and (2)
var s = '//url.com/_t_522121?location=50457347';
var n = s.lastIndexOf('/');
var result = s.substring(n + 1);
alert(result);
n = result.indexOf('?');
result = result.substring(0, n);
alert(result);
If you're trying to pull out the value after the last / and before the ? you could try something like this
url.replace(/\?.*/g, '').match(/.*\/.*(_t_.*)/)[1]
The replace removes everything from the ? and forward, the match creates a group of the values found after the last forward slash which is then accessed by the index 1
Hope this helps.
here is the jsfiddle to show it works:
My JsFiddle
I am trying to redirect one url using JavaScript for passing values to Java code using
location.href = "/something/uId=" + Idvalue + "time=" + "timeValue"
Here I am checking from two radio buttons that are today and Week radio buttons.
It is working fine for week and passing id and time value i.e., 200(example), week(Example).
But when it comes to today option it is passing only time value only. I tried the following ways
location.href += "&uId=" + uId;
// and
location.href += "time=" + today + "&uId=" + uId;
But it is passing only today only. I was surprised to see additional variables timeframe=today&x=26&y=4 but I did't pass these x and y values.
I believe that x and y variables comes because you have image button.
About your redirect page, try as follows:
location.href = '/something?uId='+ Idvalue +'&time='+ TimeValue;
It's hard to understand exactly what has gone wrong here. If uId is not being passed, I think that it is a different area of the code (whatever is responsible for setting uId that has broken.
The way you are forming URLs seems to be incorrect. The queryString of a URL should look like this:
url.com?firstArgName=firstArgValue&secondArgName=secondArgValue& ....
So your initial line should look like this:
location.href="/something?uId="+Idvalue+"&time="+timeValue
If your webserver is working in a standard way.
I need a javascript bookmarklet which can click on a button. The thing is, there are 100+ buttons on the page all with the same value. The name is unique but quite long.
The full name of the element is something like :
actions[http://apps.facebook.com/frontierville/giftaccept.php?next=giftaccept.php&senderId=1%3A1325206719&gh=3a8bfdace76051752a9127d1f9b43872&gift=nails×tamp=1285598414&ref=tab&key=29b15e06ed9d7c00a8870c955ab938cf%24%24cfH1PUUZ%217bZYhg8M-o-XQc%218HHRMcvvyhuf4d%21.64qEvlQe&src=request&aff=gift&crt=nails&signature=6dd3fa03fe88f98b6dcab4faf4c7da94]
The value of every button is Accept and Play.
So. Is there a way to have it click on the button with a specific URL in the name?
Here is the source of the info for one of the buttons (got this from chrome's inspect element feature):
<input value="Accept and Play" type="submit" name="actions[http://apps.facebook.com/onthefarm/giftaccept.php?senderId=1259413693&gift=mysterygift×tamp=1285599906&ref=gift_accept_tab&key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&signature=32db959ce43f8330cf8fd992fbd53a51&srcapp=FarmVille]">
This should do it...
javascript:var nam=prompt("Give me a URL to look for"); nam="actions["+nam.replace(/\&/g, "&")+"]"; var els=document.getElementsByName(nam); if(els.length == 0) alert("Button not found"); else els[0].click();
It's based on getElementsByName, here it is all spelled out...
var nam = prompt("Give me a URL to look for");
nam = "actions[" + nam.replace(/\&/g, "&") + "]";
var els = document.getElementsByName(nam);
if(els.length == 0)
alert("Button not found");
else
els[0].click();
Here's a rough example of what you might want to do.
var url = 'http://reallylong.facebook.url.from.your.example';
var searchName = 'actions[' + url + ']';
var items = document.getElementsByName(searchName);
if (items.length > 0) {
var myButton = items[0]; // assuming the first item is the correct one
myButton.click(); // programmatically click it
}
if the url is going to change every time, you can find someway to fill the url variable, and use that to generate the element name. This example assumes that element is the only one on the page with that exact name attribute.
This example is pretty rigid and may not work as your bookmarklet if you need to interact with it. What do the other elements look like? Would it be better to look for an element pointing to the giftaccept url or something like that? The script would have a lot more flexibility in a situation like that.
For convenience and compatibility use JQuery selectors:
nameVal = 'actions[http://apps.facebook.com/onthefarm/giftaccept.php?senderId=1259413693&gift=mysterygift×tamp=1285599906&ref=gift_accept_tab&key=78fcc7de3b36b8f9564262fab506893f%24%24ceK5RVRY61bZYhg8M-o-XQcyL%2CzHccEwEeuj4e-%21-dh0AD0A2AgyScd&signature=32db959ce43f8330cf8fd992fbd53a51&srcapp=FarmVille]'
$("input[value='Accept and Play'][name='" + nameVal + "']").click()