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

  • Home
  • SEARCH
  • 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 7812429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:30:03+00:00 2026-06-02T04:30:03+00:00

Im creating a site who works with ajaxRequest, when I click a link, it

  • 0

Im creating a site who works with ajaxRequest, when I click a link, it will load using ajaxRequest. When I load for example user/login UserController actionLogin, I renderPartial the view with processOUtput to true so the js needed inside that view will be generated, however if I have clientScriptRegister inside that view with events, how can I avoid to generate the scriptRegistered twice or multiple depending on the ajaxRequest? I have tried Yii::app()->clientScript->isSCriptRegistered('scriptId') to check if the script is already registered but it seems that if you used ajaxRequest, the result is always false because it will only be true after the render is finished.

Controller code

if (Yii::app()->request->isAjaxRequest)
{
   $this->renderPartial('view',array('model'=>$model),false,true);
}

View Code

if (!Yii::app()->clientScript->isScriptregistered("view-script"))
   Yii::app()->clientScript->registerScript("view-script","
      $('.link').live('click',function(){
         alert('test');
     })
");

If I request for the controller for the first time, it works perfectly (alert 1 time) but if I request again for that same controller without refreshing my page and just using ajaxRequest, the alert will output twice if you click it (because it keeps on generating eventhough you already registered it once)

This is the same if you have CActiveForm inside the view with jquery functionality.. the corescript yiiactiveform will be called everytime you renderPartial.

  • 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-02T04:30:04+00:00Added an answer on June 2, 2026 at 4:30 am

    To avoid including core scripts twice

    If your scripts have already been included through an earlier request, use this to avoid including them again:

    // For jQuery core, Yii switches between the human-readable and minified
    // versions based on DEBUG status; so make sure to catch both of them
    Yii::app()->clientScript->scriptMap['jquery.js'] = false;
    Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
    

    If you have views that are being rendered both independently and as HTML fragments to be included with AJAX, you can wrap this inside if (Yii::app()->request->isAjaxRequest) to cover all bases.

    To avoid including jQuery scripts twice (JS solution)

    There’s also the possibility of preventing scripts from being included twice on the client side. This is not directly supported and slightly more cumbersome, but in practice it works fine and it does not require you to know on the server side what’s going on at the client side (i.e. which scripts have been already included).

    The idea is to get the HTML from the server and simply strip out the <script> tags with regular expression replace. The important point is you can detect if jQuery core scripts and plugins have already been loaded (because they create $ or properties on it) and do this conditionally:

    function stripExistingScripts(html) {
        var map = {
            "jquery.js": "$",
            "jquery.min.js": "$",
            "jquery-ui.min.js": "$.ui",
            "jquery.yiiactiveform.js": "$.fn.yiiactiveform",
            "jquery.yiigridview.js": "$.fn.yiiGridView",
            "jquery.ba-bbq.js": "$.bbq"
        };
    
        for (var scriptName in map) {
            var target = map[scriptName];
            if (isDefined(target)) {
                var regexp = new RegExp('<script.*src=".*' +
                                        scriptName.replace('.', '\\.') +
                                        '".*</script>', 'i');
                html = html.replace(regexp, '');
            }
        }
    
        return html;
    }
    

    There’s a map of filenames and objects that will have been defined if the corresponding script has already been included; pass your incoming HTML through this function and it will check for and remove <script> tags that correspond to previously loaded scripts.

    The helper function isDefined is this:

    function isDefined(path) {
        var target = window;
        var parts = path.split('.');
    
        while(parts.length) {
            var branch = parts.shift();
            if (typeof target[branch] === 'undefined') {
                return false;
            }
    
            target = target[branch];
        }
    
        return true;
    }
    

    To avoid attaching event handlers twice

    You can simply use a Javascript object to remember if you have already attached the handler; if yes, do not attach it again. For example (view code):

    Yii::app()->clientScript->registerScript("view-script","
      window.myCustomState = window.myCustomState || {}; // initialize if not exists
      if (!window.myCustomState.liveClickHandlerAttached) {
        window.myCustomState.liveClickHandlerAttached = true;
        $('.link').live('click',function(){
           alert('test');
        })
      }
    ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a site who's users will include customers and different levels of Admins.
I am currently creating a site(php,css,html,ajax,MySql) which will have heavy user usage of space(regarding
I am creating a site and I'm using sessions to manage the user log
I'm a newbie who is creating a lightweight photo showcase site written on the
I am creating site with php. On localhost all works well. On my hosting
I creating a site in JRuby on Rails (JRuby 1.3.0 ). I am using
I'm creating new Site Definitions using this method: http://weblogs.asp.net/paulballard/archive/2007/04/09/creating-a-custom-sharepoint-2007-portal-site-definition-using-the-portalprovisioningprovider-class.aspx and when they get created,
I'm creating a site using the Twitter Bootstrap , but I've discovered that the
I'm creating a static site generator with a dynamic admin backend for one user.
Who is the acting user for cron updates drupal when creating a triggered rule?

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.