Nopcommerce 2.30 - How to add javascript

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I see nopcommerce is now in cshtml.
This is new to me and am wondering how and where you can insert javascript to what was formally a master page.

I need to attach an external js and also a load event script to the index page.

Any help would be appreciated.
12 years ago
Please have a look at the 'Nop.Plugin.ExternalAuth.OpenId' plugin. It has an example of how to add custom CSS and JS:
Html.AddCssFileParts(@Url.Content("~/Content/mycustomstyles.css"));
Html.AddScriptParts(@Url.Content("~/Scripts/mycustomscripts.js"));
12 years ago
Hello,

I wish to do something similar, including a javascript file and css file for the listexpander menu controls.

<link href="listexpander/listexpander/listexpander.css" rel="stylesheet" type="text/css" media="screen" />
<script type="text/javascript" src="listexpander/listexpander/listexpander.js"></script>


Are you suggesting I need to create a plugin for this? I have looked through the mentioned plugin but don't really understand what to do. I can't even see it including any extra scripts.

Thank you for your time,

Russ
12 years ago
It took me a while to work this out and no one was quick to assist so I understand.
You can add Css or Javascript links to your head tag by doing the following:

1. Open
/Themes/YourTheme/Views/Shared/Head.cshtml

This allows you to add custom links to your website per theme.
To do it for all themes you will need to do it in the main head here:

/Views/Shared/Head.cshtml

2. Add your desired links in this format:

<link href="@Html.ResolveUrl("~/listexpander/listexpander/listexpander.css")" rel="stylesheet" type="text/css" />
<script href="@Html.ResolveUrl("~/listexpander/listexpander/listexpander.js")" type="text/javascript" />

And your done.

~ represents your website root.
You can add your listexpander css content to your current theme css instead of adding a new file to make it easier.

If your site experiences issue after changing the head tag you may have to recompile your website.

Hope this is helpful to you and others.
12 years ago
Wonderful, thank you DTK! Thanks for taking the time to explain it so well, I hadn't thought to look in the theme area :)
12 years ago
try this plugin https://www.nopcommerce.com/p/389/google-adsense.aspx you can paste your script and add it to any part of the page including head.
11 years ago
Ok, the second method won't work in version 2.65 (just trashes the CSS completely)

The first method (for you newbies like me) should look like the following:

@{ Html.AddScriptParts(@Url.Content("~/Scripts/mycustomscripts.js")); }

I had forgotten the @{ ... } at first (noob to MVC)
11 years ago
beefydog wrote:
Ok, the second method won't work in version 2.65 (just trashes the CSS completely)

The first method (for you newbies like me) should look like the following:

@{ Html.AddScriptParts(@Url.Content("~/Scripts/mycustomscripts.js")); }

I had forgotten the @{ ... } at first (noob to MVC)


Thank you x 1000!! :)
10 years ago
I'm trying to ad the Google Adsense javascript to my 2.80 nopC site.  I've tried the ideas in this thread but no go. Google gave me script that looks similar to this, that I must include on my site:

<script type="text/javascript">
<!--
google_ad_client = "ca-pub-xxxxxxxxx";
/* ATSAd1 */
google_ad_slot = "xxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

I want to put the ad at the top of every page on my site, so I want this script in the <head/> of every page.

The last thing I tried was to put this script in a separate file, GoogleAdsense.js.  Then I added the following line to Head.cshtml:
@{ Html.AddScriptParts(@Url.Content("~/Themes/DefaultClean/Content/GoogleAdsense.js")); }

When I run that, I get an ArgumentException in the GenerateScripts() method in PageHeaderBuilder.cs:
"The URL '/Themes/DefaultClean/Content/GoogleAdsense.js' is not valid. Only application relative URLs (~/url) are allowed."

Can someone assist?
10 years ago
Brillo wrote:
I'm trying to ad the Google Adsense javascript to my 2.80 nopC site.  I've tried the ideas in this thread but no go. Google gave me script that looks similar to this, that I must include on my site:

<script type="text/javascript">
<!--
google_ad_client = "ca-pub-xxxxxxxxx";
/* ATSAd1 */
google_ad_slot = "xxxxxxx";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

I want to put the ad at the top of every page on my site, so I want this script in the <head/> of every page.

The last thing I tried was to put this script in a separate file, GoogleAdsense.js.  Then I added the following line to Head.cshtml:
@{ Html.AddScriptParts(@Url.Content("~/Themes/DefaultClean/Content/GoogleAdsense.js")); }

When I run that, I get an ArgumentException in the GenerateScripts() method in PageHeaderBuilder.cs:
"The URL '/Themes/DefaultClean/Content/GoogleAdsense.js' is not valid. Only application relative URLs (~/url) are allowed."

Can someone assist?



You don't need to use @Url.Content.
Just Html.AppendScriptParts("~/Themes/DefaultClean/Content/GoogleAdsense.js");
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.