MVC3 Url.Action querystring generation - asp.net-mvc

I am trying to generate an url for an MVC 3 action within javascript environment (in a cshtml file).
<script type="text/javascript">
...
var src = "#Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 })";
$(document.createElement("img")).attr("src", src);
...
</script>
Now this works almost fine, my problem is that the querystring is being escaped.
Instead of:
"/Products/GetProductImage?productId=1&pos=0&size=0"
it generates:
"/Products/GetProductImage?productId=1&pos=0&size=0"
so my action does not get called.
Now I know I can make my own custom Url helper function, but I was wondering if I can use this or some other built in helper to get the unescaped URL?
Thanks in advance, G.

<script type="text/javascript">
var src = "#Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";
$(document.createElement("img")).attr("src", src);
</script>

var src = "#Html.Raw(Url.Action("GetProductImage", new { productId = Model.Product.Id, pos = 1, size = 0 }))";
Url.Action worked for me not HtmlUrl.Action
Enjoy!

Related

Find and Replace in email ids to mailto link in thymleaf

I would like to know email ids in thymleaf template to mailto link
eg : hello#gmail.com to
It should use regex for finding email IDs.
I have tried with thymleaf inline javascript but unfortuantely not working in outlook.
<script>
/*<![CDATA[*/
function formatEmails(){
var content=document.getElementById('content').innerHTML;
var regex = /\S+#\S+\.\S+/g
var matches = [];
var match = regex.exec(content);
var formatedContent=content;
while (match != null) {
if(matches.indexOf(match[0]) === -1)matches.push(match[0]);
match = regex.exec(content);
}
matches.forEach(function(email,index){
formatedContent=formatedContent.split(email).join(''+email+'')
});
document.getElementById('content').innerHTML=formatedContent;
}
/*]]>*/
</script>
If there is no direct way to do this, how to make it?
Thanks in advance.

AngularJS Error: [$controller:ctrlreg] Controller is not registered

StackOverflow Peeps,
I'm having a bit of an issue with the error referenced in the title. I've scoured AngularJS documentation, previously written code, and existing StackOverflow question/answers, all to no avail. To start, here is a previously written Angular controller that works correctly:
Correct, functioning controller
And here is the malfunctioning controller:
Malfunctioning controller
What is particularly frustrating is that I quite literally copied my code from the functioning Angular controller ("focus_groups_ctrl") into my new controller ("agencies_ctrl"), and simply updated the variable/argument names to reflect this. What am I missing here? The JSON data being rendered in api/v1/agencies is working correctly, and my HTML page is being correctly "wrapped" in ng-app and ng-controller attributes. Please let me know, and feel free to ask away! Thanks!
Here is the malfunctioning code:
/* global angular */
(function() {
"use strict";
angular.module("app", ["ngAnimate"]).controller("agencies_Ctrl", function($scope, $http) {
$scope.setup = function() {
$http.get('/api/v1/agencies' + window.location.search).then(function(response) {
$scope.agenciesOffset = 0;
$scope.agenciesCount = 5;
$scope.agencies = response.data;
console.log($scope.agencies);
});
};
var i = 5;
$scope.nextLoad = function() {
$scope.agenciesOffset = i;
i += 5;
};
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
And here is the functioning code:
/* global angular */
(function() {
"use strict";
angular.module("app", ["ngAnimate"]).controller("focus_groups_Ctrl", function($scope, $http) {
$scope.setup = function() {
$http.get('/api/v1/focus-groups' + window.location.search).then(function(response) {
$scope.focusGroupOffset = 0;
$scope.focusGroupCount = 5;
$scope.focusGroups = response.data;
console.log($scope.focusGroups);
});
};
var i = 5;
$scope.nextLoad = function() {
$scope.focusGroupOffset = i;
i += 5;
};
});
}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
An unknown controller error can also be caused by accidentally redefining a module using the angular.module API, as shown in the following example.
angular.module("app", ["ngAnimate"]).controller("agencies_Ctrl", fn1);
angular.module("app", ["ngAnimate"]).controller("focus_groups_Ctrl", fn2);
To fix this problem, make sure you only define each module with the angular.module(name, [requires]) syntax once across your entire project. Retrieve it for subsequent use with angular.module(name). The fixed example is shown below.
angular.module("app", ["ngAnimate"]);
angular.module("app").controller("agencies_Ctrl", fn1);
angular.module("app").controller("focus_groups_Ctrl", fn2);

Adwords conversion tracking on link clicks that open in a new tab

I want to track the conversion for links in adwords; For this i have the classic code that looks like this(don't worry about values for conversion_id and conversion_label):
<!-- Google Code for Joc Sloturi2 Conversion Page
In your html page, add the snippet and call
goog_report_conversion when someone clicks on the
chosen link or button. -->
<script type="text/javascript">
/* <![CDATA[ */
goog_snippet_vars = function() {
var w = window;
w.google_conversion_id = xxxxxxx;
w.google_conversion_label = "dsadsadsadsadadsa";
w.google_conversion_value = dsadasda;
w.google_conversion_currency = "RON";
w.google_remarketing_only = false;
}
// DO NOT CHANGE THE CODE BELOW.
goog_report_conversion = function(url) {
goog_snippet_vars();
window.google_conversion_format = "3";
var opt = new Object();
opt.onload_callback = function() {
if (typeof(url) != 'undefined') {
window.location = url;
window.open(url, '_blank')
}
}
var conv_handler = window['google_trackConversion'];
if (typeof(conv_handler) == 'function') {
conv_handler(opt);
}
}
/* ]]> */
</script>
<script type="text/javascript"
src="//www.googleadservices.com/pagead/conversion_async.js">
</script>
After that the outbound links are looking like this
Link text whatever
My problem with this is that when I click on the link it opens the link in a new tab and also in the same tab (basically it opens the link twice); Is there a way to only open the link in a new tab in the browser and also track the conversion for it?
I know this is a bit old, but I found a solution. Remove
window.location = url;

Turbolinks and 3d party plugins issue?

I have website
http://www.optimapo.ru/
and I have some 3d party scripts installed:
live chat (https://www.jivochat.com/) and yandex analitics.
But because of turbolinks they don't work properly.
For example live chat appears only on main page. But when we go to another page which is loaded using turbolinks it dissapears.
I include scripts in my code before closing body tag
<!-- BEGIN JIVOSITE CODE {literal} -->
<script type='text/javascript'>
(function(){ var widget_id = '2qq06akKwZ';var d=document;var w=window;function l(){
var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = '//code.jivosite.com/script/widget/'+widget_id; var ss = document.getElementsByTagName('script')[0]; ss.parentNode.insertBefore(s, ss);}if(d.readyState=='complete'){l();}else{if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();</script>
<!-- {/literal} END JIVOSITE CODE -->
<!-- Yandex.Metrika counter --> <script type="text/javascript"> (function (d, w, c) { (w[c] = w[c] || []).push(function() { try { w.yaCounter39034390 = new Ya.Metrika({ id:39034390, clickmap:true, trackLinks:true, accurateTrackBounce:true, webvisor:true, trackHash:true, ut:"noindex" }); } catch(e) { } }); var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function () { n.parentNode.insertBefore(s, n); }; s.type = "text/javascript"; s.async = true; s.src = "https://mc.yandex.ru/metrika/watch.js"; if (w.opera == "[object Opera]") { d.addEventListener("DOMContentLoaded", f, false); } else { f(); } })(document, window, "yandex_metrika_callbacks"); </script> <noscript><div><img src="https://mc.yandex.ru/watch/39034390?ut=noindex" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- /Yandex.Metrika counter -->
</body>
So what do I do to make scripts work with turbolinks?
I don't think that this is strictly possible with how the snippet you include is implemented. Let's look at it, I put it over multiple lines to make it easier to read, but this is all in there:
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = '//code.jivosite.com/script/widget/'+widget_id;
var ss = document.getElementsByTagName('script')[0];
ss.parentNode.insertBefore(s, ss);
If you look at turbolinks compatibility, one of the issues it reports are self loading scripts appending to head: http://reed.github.io/turbolinks-compatibility/
I don't know where document.getElementsByTagName('script')[0] lives on your page, but there's good chance it's in the head, since it's the first script on the page. Can you change to snippet to insert at the end of the body instead?
I've managed to find a solution that actually works in Turbolinks 5. I've added this piece of code s.setAttribute('data-turbolinks-track', 'reload'); in between existing code, like this:
s.type = 'text/javascript'; s.setAttribute('data-turbolinks-track', 'reload'); s.async = true;
Side note: I've also submitted a PR with this solution to Turbolinks Compatability repository
Turbolinks overrides the normal page loading process and sometimes scripts needs refresh. Here is the rails guide for using tubolinks on page load. It is coffescript. Here is javascript equivalent :
$(document).on("turbolinks:load", function() {
// your code here
});
Below code snippet works for me.
var ready;
ready = function() {
...your code ...
};
$(document).ready(ready);
$(document).on('page:load', ready);
You can try using this gem in your application:
gem 'jquery-turbolinks'
Follow the set-up instructions here.
Place your Metrika and JivoSite scripts onto the bottom of the page. You do not need to require them explicitly.
This works for me, just change
jivo_id = 'ID-MY-ACCOUNT';
Source: https://gist.github.com/Brunomm/5c227e6c254332290812
jQuery(document).on('page:load', runChat);
jQuery(document).ready(function($) {runChat();});
// Chat online jivo
function runChat(){
$('#jivo-iframe-container, .jivo_shadow, [src*="//code.jivosite"]').remove();
delete(window.jivo_api);
delete(window.jivo_config);
window.jivo_magic_var = undefined;
window.$jivosite = null;
(function(d,s){
var z = $jivosite=function(c){ z._.push(c) },
el_script = z.s = d.createElement(s),
e=d.getElementsByTagName(s)[0],
jivo_id = 'ID-MY-ACCOUNT';
z.set=function(o){
z.set._.push(o)
};
z._=[];
z.set._=[];
$.async = !0;
el_script.setAttribute("charset","utf-8");
el_script.src='//code.jivosite.com/script/widget/'+jivo_id;
z.t=+new Date;
el_script.type="text/javascript";
e.parentNode.insertBefore(el_script,e)
})(document,"script");
}

gmaps4rails: How to implement a clickable link on a marker

Using Rails 3.2.13, ruby 1.9.3p374 and Gmaps4rails 1.5.6.
I have been strugling with something that, at first, I thought should be fairly straight forward: I want to display a marker that, when clicked, will redirect to a particular path, loading that path in the same window.
Gmaps4rails has no easy option for that in their description. So I spent countless hours searching the web, and the two best hits I found where here in StackOverflow: this smells like the right path, but this one seems also useful.
In the end, my controller looks like:
#gmapsjson = current_user.houses.all.to_gmaps4rails do | house, marker |
marker.title house.reference
marker.json({:link => polymorphic_url(house, :routing_type => :path)})
end
This is correctly (I think) generating the following in the processed html:
<script src="//maps.google.com/maps/api/js?v=3.8&sensor=false&client=&key=&libraries=geometry&language=&hl=&region=" type="text/javascript"></script>
<script type="text/javascript">
Gmaps.map = new Gmaps4RailsGoogle();
Gmaps.load_map = function() {
Gmaps.map.map_options.auto_zoom = false;
Gmaps.map.map_options.zoom = 3;
Gmaps.map.initialize();
Gmaps.map.markers = [{"title":"First House","link":"/houses/1","lat":-3.4671425,"lng":12.5264373},{"title":"Second House","link":"/houses/2","lat":-4.5543296,"lng":-3.4151647}];
Gmaps.map.create_markers();
Gmaps.map.adjustMapToBounds();
Gmaps.map.callback();
};
Gmaps.oldOnload = window.onload;
window.onload = function() { Gmaps.triggerOldOnload(); Gmaps.loadMaps(); };
</script>
Markers appear on the right places. But now, interpreting the described on that first relevant reference, I type directly on view, making sure it will appear below all the other javascripts Gmaps4Rails dumps in the resulting html:
<script type="text/javascript">
function redirect_to(url) {
window.location = url
};
Gmaps.callback = function() {
for (var i = 0; i < Gmaps.map.markers.length; ++i) {
google.maps.event.addListener(Gmaps.map.markers[i].google_object, 'click', redirect_to(Gmaps.map.markers[i].link));
}
};
</script>
But now, whenever I load the page, my Firefox javascript console will say:
TypeError: Gmaps[load_function_name] is not a function
for (key in Gmaps) {
value = Gmaps[key];
searchLoadIncluded = key.search(/load/);
if (searchLoadIncluded === -1) {
load_function_name = "load_" + key;
_results.push(Gmaps[load_function_name]()); <=== Points to this line
} else {
_results.push(void 0);
}
}
So, any ideas on what is wrong?
Thanks in advance,
Marco
Rather than doing it in JavaScript you can just generate all the html including the links is your controller.
Note: To get the link generated correctly I set it up separately and assigned it to a variable
This is what my locations_controller.rb contains:
#json = Location.all.to_gmaps4rails do |location, marker|
location_link = view_context.link_to location.name, location_path(location)
marker.title location.name
marker.infowindow "<h4><u>#{location_link}</u></h4>
<i>#{location.address}</i>"
end
When you set it up correctly you'll get hashes generated that look something like this
{"title":"Home",
"description":"<h4><u>Place</u></h4>
<i>123 E Main St. Anytown, USA</i>",
"lat":29.585019,
"lng":-81.319479}
The wiki page here helped me. Hope this helps you

Resources