3

I have three different cases in my project for opening new window. When I click a link in a window, I expected to do one of the following solutions:

I will use following types for window open:

_blank
_self
_parent

I have the following columns for opening a new window:

Code          Width   Height   Behaviour
AccountPage    400      600      Self
ProspectPage   700      700      Blank
PasswordPage   200      200      Single

I need to pass specific parameters to javascript like width, height and code. Code is used for which screen is open and take its parameters. I have invoicepage, prospectpage,searchpage etc. If invoicepage is opened its width will be 200 or if prospectpage is opened the width will be 400px.

What is the proper way to manage and pass to javascript window open types and parameters in generic technique? How Can I prepare my classes and types?

I tried to use meta tag in my LayoutGeneral.cshtml for pass the dataset to javascript but I couldn't get it.

<meta name="IrisTabHeaderName" content="@Model.WindowInfo" />

Should I hold my data in my BaseViewModel and pass the parameters to javascript from there or should I hold my all cases in javascript and call from cshtml page with class?

tdog
  • 482
  • 3
  • 17
  • Possible duplicate of [target="\_blank" vs. target="\_new"](http://stackoverflow.com/questions/4964130/target-blank-vs-target-new) – Luke Sep 07 '16 at 15:18
  • Please see the answer [here](http://stackoverflow.com/a/8867079/894792). Just provide a name for the `target=""` attribute in your link and it will load it into the same window if the window is already opened with _the name you specify in the target_. You can likely use your `Code` value to do this. – Luke Sep 07 '16 at 15:19
  • per load. when i click a new link in current window if there is no open window for the code like PasswordPage i should open a new blank modal page but if i have already have an opened window for this link, i will not open a new blank modal window. i should open link in that window, not in another window. – tdog Sep 07 '16 at 15:20
  • A modal window, or a new tab/window? – Luke Sep 07 '16 at 15:20
  • Do you want to set a condition for window type with your logic into your `controller` or `repository` and set window type your link. Right ? – Sunil Kumar Sep 07 '16 at 15:24
  • A window with my specific width and height Luke. And yes that is exactly what i want Sunil. – tdog Sep 07 '16 at 15:26

2 Answers2

3

The way of your passing the dataset to javascript would be wrong. You can do hold your page parameters with enumeration or in a json object and then deserialize it as follows:

  var data =
'[{
    "Code": {
        "AccountPage": { "width": "400", "height": "600", "name": "_self" },   
        "ProspectPage": { "width": "700", "height": 700, "name": "_blank" },
        "PasswordPage": { "width": "200", "height": "200", "name": "PasswordPage" }
    }
}]';

add this data.json file in the script source:

<script type="text/javascript" src="data.json"></script>

and get your object from json file in javascript:

var pageRules = JSON.parse(data);

and finally you can check it with attach to 'a' tag's click event:

$(function () {
    $("a").click(MyLinkClickFunc);
});

function MyLinkClickFunc()
{
    var $this = $(this);
    var attrName = $this.attr("name");
    var params = null;

    if (typeof attrName !== typeof undefined && attrName !== false) {
        params = pageRules.Code[attrName];
        if(params == null)
        {
            var authCode = attrName.substr(0, attrName.indexOf('_'));
            params = pageRules.Code[authCode];
        }
    }

    if (params != null)
    {
        MyPopupOpener($this.attr("href"), params.name, params.width, params.height);
    }
}
utaco
  • 1,106
  • 8
  • 15
1

According to me the best approach you can use: - First you have to create Enum for your window types - Second set your property using these Enums - And Last set your property value at your .cshtml page.

I have a create a sample demo for you, It may help you:

1) Create a Enum for Window types:

public enum WindowTypes
     { _self = 1, 
       _blank = 2, 
       _parent = 3, 
       _top = 4 
      }

2) Create a property into your Base View Model

public class TestBaseViewModel
    {
        public int WindowType { get; set; }
    }

3) Set Property value into your controller or into your repository

public ActionResult Index()
        {
            TestBaseViewModel model = new TestBaseViewModel();
            model.WindowType = (int)EnumsCollection.WindowTypes._blank; // apply your logic like this
            return View(model);
        }

4) Simply get this property at your .cshtml page

@Html.ActionLink("link text", "actionName", null, new { target = Enum.GetName(typeof(EnumsCollection.WindowTypes), Model.WindowType) }) 

Then output will be as below :

 <a href="/Controller/actionName" target="_blank">link text</a>

Hope it will helps you.

Happy coding :)

Sunil Kumar
  • 3,142
  • 1
  • 19
  • 33
  • It's a good solution but what in addition to this I need pass specific parameters to javascript like width, height and code. Code is used for which screen is open and take its parameters. I have invoicepage, prospectpage,searchpage etc. If invoicepage is opened its width will be 200 or if prospectpage is opened the width will be 400px. – tdog Sep 08 '16 at 07:56
  • it would be better if you can post your html code and your javascript where you want to implement the same – Sunil Kumar Sep 08 '16 at 08:10