KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Add defer tag in script #411

Open mike opened this topic on on Aug 11, 2018 - 5 comments

mike commented on Aug 11, 2018

Hi , I would like to add defer tag in script ...

I tried in Widget.php....

protected function resourceSettings()
	{
		//This function will be overwritten
		// return array(
		//	"libraries"=>array("jQuery")	
		// 	"folder"=>null,
		// 	"css"=>array(),
		// 	"js"=>array(),
		// );
		return null;
	}

	public function renderResources($settings=null,$return=false)
	{

		if($settings==null)
		{
			$settings = $this->resourceSettings();
		}
		else if(gettype($settings)=="boolean")
		{
			$return = $settings;
			$settings = $this->resourceSettings();
		}
		
		if($settings && isset($settings["folder"]))
		{
			$this->getAssetManager()->publish($settings["folder"]);
			
			$css = Utility::get($settings,"css",array());
			$js = Utility::get($settings,"js",array());
			$library = Utility::get($settings,"library",array());
			$html = "";
			foreach($library as $libName)
			{
				switch(strtolower($libName))
				{
					case "jquery":
						$publicAssetUrl = $this->getReport()->publishAssetFolder(realpath(dirname(__FILE__)."/../clients/jquery"));
						$html.= str_replace("{jsUrl}",$publicAssetUrl."/jquery.min.js","<script src='{jsUrl}' type='text/javascript' defer></script>");		
					break;
				}
			}
			foreach($css as $cssFile)
			{
				$html.= str_replace("{cssUrl}",$this->getAssetManager()->getAssetUrl($cssFile),"<link href='{cssUrl}' rel='stylesheet' type='text/css'></link>");
			}
			foreach($js as $jsFile)
			{
				$html.= str_replace("{jsUrl}",$this->getAssetManager()->getAssetUrl($jsFile),"<script src='{jsUrl}'  type='text/javascript' defer></script>");
			}
			if($return)
			{
				return $html;
			}
			else
			{
				echo $html;
			}
		}
	}

but its not showed when I view page source .......

David Winterburn commented on Aug 13, 2018

Hi Mike,

Please try to edit the file ResourceManager.php as well. Thanks!

mike commented on Aug 13, 2018

yes I edited both file ResourceManager.php -

protected function addScript($script,$options=array(),$at='begin')

{
    $options["type"] = Utility::get($options,"type","text/javascript");
    $options["defer"] = Utility::get($options,"defer","defer");
    $this->tags[md5($script)] = array(
        "at"=>$at,
        "tag"=>"script",
        "options"=>$options,
        "content"=>$script,
    );
    return $this;
}
David Winterburn commented on Aug 14, 2018

If your script link to a source file, please edit this function in ResourceManager.php instead:

protected function addScriptFile($src,$options=array(),$at) {
...
}

Thanks!

mike commented on Aug 14, 2018

Thanks It got add but

<script type='text/javascript'

defer='defer'

src='https://192.168.1.100/newlive/koolreport_assets/5941336600/jquery.min.js'></script>

this format .....

and I NEED IT BELOW FORMAT <script type='text/javascript'

src='https://192.168.1.100/newlive/koolreport_assets/5941336600/jquery.min.js'
defer ></script>

David Winterburn commented on Aug 16, 2018

Hi Mike,

To create an tag attribute alone, please set its value to be an empty string like this:

$options["defer"] = Utility::get($options,"defer","");

Let me know if it works like what you want. Thanks!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

None