The base rails app has flash messages that can be called via:
"flash.now[:error]"
However, how do I call it manually via JS? Here is my current code:
var show_error = function (message) {
$("#flash-messages").html('<div class="flashes"> <div class="flash-notice">' + message + '</div> </div>');
return false;
};
You can set a default message by including it in the function. Read more about JS || here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR_()
You may also want to see this answer SO: Best practice method of displaying flash messages
var show_error = function (message) {
message = message || 'Something should be flashing here!';
$("#flash-messages").html('<div class="flashes"> <div class="flash-notice">' + message + '</div> </div>');
return false;
};
/*
show_error();
*/
/*
show_error('Something is Flashing!');
*/
Related
i am developing an application and loading an hosted application using the inapp browser plugin cordova-plugin-inappbrowser
I have gotten most of the functionalities to work but i am unable to get the part of loading an error message when he url does not load, it dosent just work and shows me an error message of the url where i have hosted my application instead.
Which could be embarrassing.
please i need help on this
This is what am working with below thanks for ur potential responses
// my child browser code, the main source of my app content
function fire(){
var ref = cordova.InAppBrowser.open('http://####################', '_blank', 'location=no,zoom=no,hardwareback=yes,clearsessioncache=yes,clearcache=no');
var myCallback = function(event) { alert(event.url); }
ref.addEventListener('loadstart', inAppBrowserbLoadStart);
ref.addEventListener('loadstop', inAppBrowserbLoadStop);
ref.addEventListener('loaderror', loadErrorCallBack);
ref.addEventListener('exit', inAppBrowserbClose);
}
function loadErrorCallBack(params) {
$('#status-message').text("");
var scriptErrorMesssage =
"alert('Sorry we cannot open that page. Message from the server is : "
+ params.message + "');"
inAppBrowserRef.executeScript({ code: scriptErrorMesssage }, executeScriptCallBack);
inAppBrowserRef.close();
inAppBrowserRef = undefined;
}
function executeScriptCallBack(params) {
if (params[0] == null) {
$('#status-message').text(
"Sorry we couldn't open that page. Message from the server is : '"
+ params.message + "'");
}
}
Your code is generally fine, but you have no control over the title of the alert() function. You can use some other techniques to display the error message. For example, you can use a div:
function loadErrorCallBack(params) {
$('#status-message').text("");
var scriptErrorMesssage = createMsg('Sorry we cannot open that page. Message from the server is: '
+ params.message);
inAppBrowserRef.executeScript({
code: scriptErrorMesssage
}, executeScriptCallBack);
inAppBrowserRef.close();
inAppBrowserRef = undefined;
}
function createMsg(msg) {
return 'var div = document.createElement("div");'
+ 'div.style.position = "absolute";'
+ 'div.style.top = "50%";'
+ 'div.style.left = "50%";'
+ 'div.style.width = "100px";'
+ 'div.style.height = "100px";'
+ 'div.style.color = "#f00";'
+ 'div.innerHTML = "' + msg + '";'
+ 'document.appendChild(div);'
}
I've begun playing around with SignalR, of course starting with the initial chat hub that I suppose everybody does at one point or another. I want to modify it so that if the user types in HTML in their message, when it gets displayed it shows rendered HTML as oppose to just the string with HTML tags in it.
Here is my javascript:
<script type="text/javascript">
$(function () {
var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (name, message) {
var encodedName = $('<div />').text(name).html();
var encodedMesg = $('<div />').text(message).html();
if (message === "joined session") {
$('#discussion').append('<li><strong>' + encodedName + ' ' + encodedMesg + '</strong></li>');
} else {
$('#discussion').append('<li><strong>' + encodedName + '</strong>:  ' + encodedMesg + '</li>');
}
};
$('#message').focus();
$.connection.hub.start().done(function () {
chat.server.send("#FullName", "joined session");
$('#sendmessage').click(function () {
chat.server.send("#FullName", $('#message').val());
$('#message').val("").focus();
});
});
});
The encodedMesg has "this is <b>bold</b>", but instead of rendering it as HTML, it just shows it as a string. How can I allow this to render as HTML?
I've tried encoding the < as < and > as > but that didn;t work. I also tried %3C and %3E but they didn;t work either.
Calling .text changes the text of an object and intentionally prevents html or scripts from being parsed.
You can get it to parse it by changing the value with .html
var encodedMesg = $('<div />').html(message);
use this:
var encodedMsg = $('').text(message).html();
This is my Signal R Client.
I get this error when i run my client.(0x800a139e - JavaScript runtime error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. .)
The exception comes from line $.connection.hub.start
There is a ServerHub class in a folder HUBS in my Server application which runs fine.
Can anyone help me out..
Thanks
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>
var ChatHubProxy = $.hubConnection("http://localhost:39670/MySignalRServer/signalr/hubs");
var chat = ChatHubProxy.createHubProxy('ServerHub');
chat.on("addNewMessageToPage",function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
});
$.connection.hub.start({jsonp:true}).done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
alert("hiii");
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
Try change your code to :
<script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>
var chat = $.connection.ServerHub; //here
chat.on("addNewMessageToPage", function(name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
});
$.connection.hub.start().done(function() { //here
$('#sendmessage').click(function() {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
alert("hiii");
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
And make sure that this address is good - <script src="http://localhost:39670/MySignalRServer/signalr/hubs"></script>
I always use this - <script src="/signalr/hubs"></script>
And add HubName Attribute
[HubName("ServerHub")]
public class ServerHub : Hub
{
public string Send(string name, string message)
{
Clients.All.broadcastMessage(name, message);
return null;
}
}
or change this code :
var chat = $.connection.ServerHub;
to
var chat = $.connection.serverHub;
Demo project : https://github.com/czerwonkabartosz/SignalRDemo
This question already has an answer here:
How to set a C# variable value from javascript
(1 answer)
Closed 7 years ago.
I have my ASP.NET C# Web site and i have some JavaScript script inside one of my pages, i already succeeded in calling a C# function from my Code Behind but i want to send some variable from my JavaScript script as an argument of the function.
Here is my code, including the C# function call:
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div /> ').text(message).html();
var tremp_id = $('<div /> ').text("<%=Request.QueryString["trempid"]%>").html();
// Add the message to the page.
$('#discussion').append('<li class="<%=returnLiClass()%><strong>' + encodedName
+ '</strong>: ' + encodedMsg + "Tremp:" + tremp_id + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val('<%=returnName()%>');
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
</script>
As you can see I'm calling the 'returnLiClass()' function (line 13) and i want to send the 'encodedMsg' var inside it.
How can i do it? Thanks!
Place a function with name same as client method in your Hub class.
void broadcastMessage(string name, string message)
{
//Do something here.....
}
Please note that the parameter name must be same.
I have the following function in a phantomjs script:
page.injectJs("jquery.js");
var message = null;
page.evaluate(function(message) {
message = $('mydiv').text()
console.log('message inside' + message );
},message);
console.log('message is ' + message );
inside the function the correct message is logged. outside I get:
message is null
What am I doing wrong?
Is "mydiv" an id of div? In that case you should change your jquery selector to:
$('#mydiv').text()