|
|
#1 |
![]() Join Date: Sep 2008
Reputation: 838
Posts: 6,338
|
JavaScript help
I'm trying to make a simple GreaseMonkey UserScript -- so simple, in fact, that I had wanted to make it as a Stylish UserStyle instead, but alas, the way the page in question is designed prohibits this approach.
At any rate, I only recently started teaching myself JavaScript -- then realized that most of my knowledge of HTML is over a decade old and decided I should brush up on that first. I haven't quite gotten back to the actual JavaScript part of my studies yet, and basically all I know is document.write(""); and document.getElementById("").innerHTML="";, so I would appreciate it if anyone could help with the following. Basically, what I need to do is search a page for ALL instances of a specific element with a specific attribute (eg: <div class="example">), which contains a specific string (eg: <a href="http://www.google.com/">), and then change the original element to have a different attribute (eg: <div class="example2">). Shouldn't be too hard, I think. If I could use PHP it would just be a single preg_replace();. |
|
|
|
|
|
#2 |
![]() Join Date: Apr 2008
Reputation: 390
Posts: 671
|
Do you mean this:
Code:
var n = searchString.indexOf(stringToSearchFor);
if (n >= 0) {
searchString = searchString.substring(0, n) + replacementString + searchString.substring(n + stringToSearchFor.length);
}
Last edited by bessent: 04-29-2011 at 10:54 AM. |
|
|
|
|
|
#3 |
![]() Join Date: Feb 2009
Reputation: 3980
Posts: 2,876
|
Yep, google tutorials and help for indexOf() - That will help you do what you need.
|
|
|
|
![]() |
|
||||||
| Thread Tools | |
| Display Modes | |
|
|