Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:29:41+00:00 2026-06-11T21:29:41+00:00

I’m looking for different methods of injecting JavaScript into a WebView (in Cocoa). I

  • 0

I’m looking for different methods of injecting JavaScript into a WebView (in Cocoa).

I am trying to inject some javascript into the <head> tag of a HTML file that has been loaded into the WebView.

The following method doesn’t work for me. It seems to only work with very simple JavaScript with no nested brackets (from what I have tested):

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
                                                     "script.type = 'text/javascript';"
                                                     "script.text = \"some simple JavaScript"
                                                     "}\";"
                                                     "document.getElementsByTagName('head')[0].appendChild(script);"]];

Are there any other ways of doing this with more complicated JavaScript / jQuery functions?

The full code I am trying to inject is as follows:

function submitFormForTime(time){
    $(\".t_h\").each(function(i, obj) {
        if ($(this).text() != time) return;
        $(this).parent().find(\"form\").each(function(i, obj){
            obj.submit();
        });
    });
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:29:42+00:00Added an answer on June 11, 2026 at 9:29 pm

    You can’t use jQuery to add <script> tags. This has been asked many times. See:

    • Can't append <script> element
    • Can I create script tag by jQuery?

    You can do it with browser built-in functions, BUT:

    Would it work to just assign your function to a variable on your page and then evaluate it? Something like:

    -(void)webViewDidFinishLoad:(UIWebView *)webView
    {
        // add function to page:
        NSString * js = @"myFunction = function(){ return \"success!\"; }" ;
        [ webView stringByEvaluatingJavaScriptFromString:js ] ;
    
        // execute newly added function:
        NSString * result = [ webView stringByEvaluatingJavaScriptFromString:@"myFunction();"] ;
        NSLog(@"result=\"%@\"", result) ;
    }
    

    update explicit examples:

    You should use my above example as a template!

    Anyway, there’s 2 parts to the process… The Javascript part and the Objective-C part. At the Javascript level, you are doing this:

    // assign a function to the variable "someFunction":
    var someFunction = function(){...};
    
    // call the function in the variable "someFunction":
    someFunction();
    

    You can also do it without assigning the function to a variable, for example, if you don’t care about referring to the function in the future. Doing it without a variable and using an anonymous function looks like this:

    ( function(){...} )() ;
    

    i.e. instead of doing var A = <the function>; A(); you are doing (<the function>)();

    Finally, if your anonymous function takes arguments, like yours does, the pattern looks like this:

    ( function(var0, ..., varN){...} )(arg0, ..., argN);
    

    Using that as a template for your code, your JS ends up like:

    ( function(time) {
        $(\".t_h\").each(function(i, obj) 
        {
            if ($(this).text() != time)  return;
    
            $(this).parent().find(\"form\").each(function(i, obj) {
                obj.submit();
            });
        });
    })( 123.456 );
    

    So, that’s what you want the web view to execute… That’s the code we pass to the webview. But you have to build that string in your Obj-C program, with the “complication” of inserting the current time argument (123.456 in the above code).

    It looks like this:

    NSString * jsTemplate =
        @"( function(time) {"
        @"        $(\".t_h\").each(function(i, obj) {"
        @"            if ($(this).text() != time)  return;"
        @"            $(this).parent().find(\"form\").each(function(i, obj) {"
        @"                obj.submit();"
        @"            });"
        @"        });"
        @"    })( %g );" ;
    NSString * js = [ NSString stringWithFormat:jsTemplate, 123.456 ] ;
    NSString * result = [ webView stringByEvaluatingJavaScriptFromString:js ] ;
    

    I think I’ve written enough now 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.