开发者

XCODE trying to search and replace in localy loaded html

开发者 https://www.devze.com 2023-03-19 13:36 出处:网络
I admit I am no Javascript expert, and this one is really giving me trouble any help is greatly appreciated

I admit I am no Javascript expert, and this one is really giving me trouble any help is greatly appreciated

this is what I got so far inside html there is javascript

<head>
<script type='text/javascript'>
function findAndReplace(searchText, replacement) 
{

    alert('pokrecemo find and replace');

        if (!searchText || typeof replacement === 'undefined') {
        // Throw error here if you want...
        alert('govno greska u parametrima')
        return;## Heading ##
    }
    var regex = typeof searchText === 'string' ?
                new RegExp(searchText, 'g') : searchText,
        childNodes = document.body.childNodes,
        cnLength = childNodes.length,
        excludes = 'head,style,meta,script';
    while (cnLength--) {
        var currentNode = childNodes[cnLength];
        if (currentNode.nodeType === 1 &&
            (excludes + ',').indexOf(currentNode.nodeName.toLowerCase() + ',') === -1) {
            arguments.callee(searchText, replacement, currentNode);
        }
        if (currentNode.nodeType !== 3 || !regex.test(currentNode.data) ) {
            continue;
        }
        var parent = currentNode.parentNode,
            frag = (function(){
                var html = currentNode.data.replace(regex, replacement),
                    wrap = document.createElement('div'),
                    frag = document.createDocumentFragment();
                wrap.innerHTML = html;
                while (wrap.firstChild) {
                    frag.appendChild(wrap.firstChild);
                }
                return frag;
            })();
        parent.insertBefore(frag, currentNode);
        parent.removeChild(currentNode);
    }
}


</script>
....
</head>

and XCODE

(void)webViewDidFinishLoa开发者_C百科d:(UIWebView *)webView {  
    if (index.row==7) {  
    script=@"findAndReplace(75,900)";  



NSLog(@"%@",script);  
[self.tekst stringByEvaluatingJavaScriptFromString:script];  

}   

}

and in viewdidload

.....

case 7:

[tekst loadRequest:[NSURLRequest requestWithURL:[NSURL `

fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"sometest" ofType:@"html
"]`isDirectory:NO]]];  




tekst.delegate = self; 



break; 

.....

what I am doing wrong

I can see that javascript is starting but it does exactly nothing!!!


Are you just trying to do a find and replace? This would be much easier to accomplish in Objective-C.

NSString *htmlString =  [NSString stringWithContentsOfFile:@"pathToFile" encoding:NSUTF8StringEncoding error:nil];

NSString *finalString = [htmlString stringByReplacingOccurrencesOfString:@"75" withString:@"900"];

Then to load the new string

[webView loadHTMLString:finalString baseURL:someURLHere];


I believe you need the semicolon after the JavaScript.

script=@"findAndReplace(75,900);";  


there was a mistake in javascript, I placed it in dreamweaver and it immediatly recognized syntax error, by removing this error everything works fine, and there should be no semicolon as suggested by picciano, it starts to behave well at least oddly, don't know why, there should be semicolon but it only works correctly without it

by removing this

   if (!searchText || typeof replacement === 'undefined') {
    // Throw error here if you want...
    alert('govno greska u parametrima')
    return;## Heading ##
}

everything works fine

original coding is here http://james.padolsey.com/javascript/find-and-replace-text-with-javascript/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号